简体   繁体   中英

How To Build Application With Precompiled Library in Visual Studio

I am working with a precompiled library (.lib) that I wrote in Visual Studio 2010. My solution has two projects. The first project (C) builds the library. The second project (C++) is a Win32 console application that is intended to test the library. Everything I've tried does not resolve the linker errors. (I thought this would be easy because everything is in the same solution.) Any pointers would be appreciated. Thanks.

Here is the linker error that I am getting:

1>MyProject.obj : error LNK2019: unresolved external symbol "void __cdecl my_function(void)" (?my_function@@YAXXZ) referenced in function _wmain

1>C:\\Documents and Settings\\user\\Desktop\\MySolution\\Debug\\MyProject.exe : fatal error LNK1120: 1 unresolved externals

This is the code for the Win32 console application:

#include "stdafx.h"
#include "my_api.h"

int _tmain(int argc, _TCHAR* argv[])
{
  my_function();

  return 0;
}

Here's how my_function is declared in my_api.h :

extern VOID my_function(VOID);

You need to declare your function with C linkage in order for the linker to understand that those functions were defined in C code, not C++ code:

// my_api.h
#ifdef __cplusplus
extern "C" {
#endif

extern VOID my_function(VOID);
// more function declarations etc.

#ifdef __cplusplus
}
#endif

If you only have one function, you can drop the braces:

// my_api.h
#ifdef __cplusplus
extern "C"
#endif
extern VOID my_function(VOID);

Ensure that the output directory of the library project (whatever $(OutDir) expands to in that project) is on the linker search path for the test project. To do this, go to the project Properties dialog. Under Linker -> General , make sure the directory is set in Additional Library Directories . Further, make sure under Linker -> Input that the library itself ( my_api.lib ) is listed in Additional Dependencies .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM