Usage of static library in C

B

bauerwo

Dear all

I'm a newbie to C, C++, and Visual C++ .Net, so if there is a better place
to ask this question, please let me know.

Using Visual C++ .Net, I haven compiled and linked a static library
"myLib.lib", providing the function "myFunc". I can access it without any
problem in another C++ program "myMain" by writing

extern double myFunc(int N);
pragma comment(lib,"myLib.lib")

But the goal would be to have the calling program be written in C; when I
change the project properties into /TC "Compile as C program", I get

myMain error LNK2019: unresolved external symbol _myFunc referenced in
function _main

What's the right way to include a library into C?

Best regards,

W. Bauer
 
D

David Lowndes

Using Visual C++ .Net, I haven compiled and linked a static library
"myLib.lib", providing the function "myFunc". I can access it without any
problem in another C++ program "myMain" by writing

extern double myFunc(int N);
pragma comment(lib,"myLib.lib")

But the goal would be to have the calling program be written in C; when I
change the project properties into /TC "Compile as C program", I get
myMain error LNK2019: unresolved external symbol _myFunc referenced in
function _main

What's the right way to include a library into C?

The library function needs to be defined as "C" linkage - see the help
on extern - "Using extern to Specify Linkage".

Dave
 
B

bauerwo

The library function needs to be defined as "C" linkage - see the help
on extern - "Using extern to Specify Linkage".

I suppose this is required when a C library is to be used in C++. But I
would like to use in a C program a library that has been written in C++. Is
this possible?

Many thanks, W. Bauer
 
R

Ronald Laeremans [MSFT]

Only if it exports C functions or if you are willing to get the C code to
compile as C++ (which in general is quite a realistic effort, even for a
relatively large code base).

Ronald Laeremans
Visual C++ team
 
B

bauerwo

Only if it exports C functions or if you are willing to get the C code
to compile as C++ (which in general is quite a realistic effort, even
for a relatively large code base).

Many thanks, now I got it; functions in a dll can be exported and thus be
made accessible by C, super!

Best regards, W. Bauer
 

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

Top