C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to link obj's from c-source with obj's from c++-source

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guido Belligoi
Guest





PostPosted: Thu Oct 19, 2006 9:10 am    Post subject: How to link obj's from c-source with obj's from c++-source Reply with quote



Hi,

I compiled 2 simple source-files with cl -c main.cpp and cl -c test.c.
test.c contains just a simple function, which is declared in test.h.
test.h is included in test.c and in main.cpp.
When I try to link the objects with link main.cpp test.c -out:prog.exe
I get the error:
"main.obj : error LNK2019: unresolved external Symbol "int __cdecl
greater(int,int)" (?greater@@YAHHH@Z), called in function _main
prog.exe : fatal error LNK1120: 1 unresolved external Symbol"

What have I to do, to be able to link those objects together?

Guido Belligoi
Back to top
Ivan Vecerina
Guest





PostPosted: Thu Oct 19, 2006 9:10 am    Post subject: Re: How to link obj's from c-source with obj's from c++-sour Reply with quote



"Guido Belligoi" <DonBlech (AT) web (DOT) de> wrote in message
news:1161244159.510082.56460 (AT) b28g2000cwb (DOT) googlegroups.com...
: Hi,
:
: I compiled 2 simple source-files with cl -c main.cpp and cl -c test.c.
: test.c contains just a simple function, which is declared in test.h.
: test.h is included in test.c and in main.cpp.
: When I try to link the objects with link main.cpp test.c -out:prog.exe
: I get the error:
: "main.obj : error LNK2019: unresolved external Symbol "int __cdecl
: greater(int,int)" (?greater@@YAHHH@Z), called in function _main
: prog.exe : fatal error LNK1120: 1 unresolved external Symbol"
:
: What have I to do, to be able to link those objects together?

A: extern "C"


For headers that are to be included both from C and C++ sources,
it is common to surround all declarations with:

#ifdef __cplusplus
extern "C" {
#endif

int greater(int a, int b);

#ifdef __cplusplus
}
#endif


You could also include an originally C-only header with:
extern "C" {
#include "test.h"
}


IIRC it is also possible to declare an individual function
as extern C:

extern "C" int greater(int a, int b);



hth-Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Back to top
Guido Belligoi
Guest





PostPosted: Thu Oct 19, 2006 9:10 am    Post subject: Re: How to link obj's from c-source with obj's from c++-sour Reply with quote



Ivan Vecerina schrieb:


a very helpfully answer!

Thank you for the fast reply - it works fine now!!!

Guido Belligoi
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.