How to link a static library in Visual C++ 2008?

前端 未结 3 680
我在风中等你
我在风中等你 2021-01-16 09:29

My VC++ solution includes two projects, an application (exe) and a static library.

Both compile fine, but fail to link. I\'m getting an \"unresolved external symbol\

3条回答
  •  萌比男神i
    2021-01-16 10:15

    How are you setting it up to link? And what does your header file for MyApplication and MyStaticLibrary::accept look like?

    If you have both projects in the same solution file, the best way to set it up to link is to right-click the Solution file->Properties and then set the library as a dependency of the application. Visual Studio will handle the linking automatically, and also make sure that the library build is up to date when you build your application.

    That error kinda sounds like you have it defined as a DLL import/export in your header file though.

    Edit: Yes, that's the problem. You probably created it as a dynamic library first? (or whoever wrote it did.)

    There are a few options.

    1) You can just delete all of that stuff, and any UDT_API modifiers in the code.

    2) You can delete that stuff and add this line:

    #define UDT_API
    

    3) A more robust solution is to change it to this:

    #ifdef  UDT_STATIC
        #define UDT_API
    #else
        #ifdef UDT_EXPORTS
           #define UDT_API __declspec(dllexport)
        #else
           #define UDT_API __declspec(dllimport)
        #endif
    #endif
    

    And then add the preprocessor directive UDT_STATIC to your projects when you want to use it as a static library, and remove it if you want to use it as a dynamic library. (Will need to be added to both projects.)

提交回复
热议问题