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

G

Guido Belligoi

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
 
I

Ivan Vecerina

: 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
 
G

Guido Belligoi

Ivan Vecerina schrieb:


a very helpfully answer!

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

Guido Belligoi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top