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

前端 未结 3 654
我在风中等你
我在风中等你 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条回答
  • 2021-01-16 10:01

    Since you're working with sockets, make sure to add WS2_32.lib in the additional dependencies of the project.

    0 讨论(0)
  • 2021-01-16 10:05

    It looks like the exported functions in the static library have the wrong calling convention. How does the header file for the exported functions look like?

    Ok, so the functions are using UDT_API as prefix. They shouldn't if you are just creating a static .lib to link against.

    An alternative is to change the lib to create a dll instead. So you create static linkage with a dll. Only problem is that you have to provide the dll with your app. Still not a bad solution, since it enables you to do fixes in the lib.

    0 讨论(0)
  • 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.)

    0 讨论(0)
提交回复
热议问题