Little Rock

I have to make a static library with a set of *.c files. Since they are used for multiple OS, the file names and contents are required to be changed as minimal as possible. When I create a static library using VS2003, the creation of the *.lib file is ok. But when an Application linked to this lib file, it will have a link error:

testApp error LNK2019: unresolved external symbol "unsigned int __cdecl FUNCTION_NAME"

I used a *.def file in the library project to specify the export function FUNCTION_NAME, but the library project doesn't have a *.cpp file. Does this cuase the link problem in the application Is possible to avoid this eeror for a library with only *.c files

Thanks,

Little Rock



Re: Visual C++ General Make a static library in Visual Studio 2003 with C++

scor7910

Hi.

I guess LNK2019 error is occure when you didn't include .lib file on your project.

Did you include lib file on project settings

Or you can include lib file on source code.

Code Block
#pragma(lib, "lib file name here");

Good luck~~






Re: Visual C++ General Make a static library in Visual Studio 2003 with C++

Simple Samples

Little Rock wrote:
when an Application linked to this lib file, it will have a link error:

testApp error LNK2019: unresolved external symbol "unsigned int __cdecl FUNCTION_NAME"

Are you familiar with:

extern "C"

If not, then that is probably what you need. Look in the documentation for details, but that is used in cpp files that call functions in c files. It is used for other things also but for you that is how you would use it.

Little Rock wrote:
I used a *.def file in the library project to specify the export function FUNCTION_NAME, but the library project doesn't have a *.cpp file. Does this cuase the link problem in the application Is possible to avoid this eeror for a library with only *.c files

Unless you are aware of documentation that says otherwise, as far as I know, def files have no affect on static libraries.

If it helps, it is possible to compile files as C++ files using compile options. The most likely cause of incompatibility is name mangling. C++ mangles names and C does not. Compiling all files as C++ will solve that problem. There is a small risk of incompatibility when a c file is compiled as C++ but hopefully it is not a problem for your application. If you must compile the c files as C source, then probably extern "C" will work.






Re: Visual C++ General Make a static library in Visual Studio 2003 with C++

Little Rock

I did include that library in my test application and the test application is calling a function of the library. The link error seems telling me the function is invisible to outside of library world.

Thanks,

Little Rock





Re: Visual C++ General Make a static library in Visual Studio 2003 with C++

Little Rock

Thank you all. The problem has been solved by adding the extern "C".

Little Rock





Re: Visual C++ General Make a static library in Visual Studio 2003 with C++

Simple Samples

Good, then you have it figured out. Note that if you need to use a header for both C and C++ it is possible to use a preprocessor symbol to help; just see the headers used by the Windows SDK for samples.