C ++ Project compiles as static lib, crash (linker error) as dynamic lib. What for?

I have my own C ++ project VS2008, which I want to compile as a DLL.

It references only one external library (log4cplus.lib) and uses its functions. (also uses log4cplus.h files, of course).

When I try to compile my project as a static library, it succeeds. When I try as a DLL, it fails:

1>MessageWriter.obj : error LNK2019: unresolved external symbol "public: static class log4cplus::Logger __cdecl log4cplus::Logger::getInstance(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > const &)" ( ?getInstance@Logger @ log4cplus@ @ SA?AV12@ABV ?$basic_string@ _WU?$char_traits@ _W@std @@ V?$allocator@ _W@2 @@ std@ @@Z) referenced in function "class log4cplus::Logger __cdecl Log(void)" ( ?Log@ @ YA?AVLogger@log4cplus @@XZ) 

There are 4 more errors related to functions in log4cplus.lib.

Something seems really stupid .. please help me :)

Thanks!

Edit:

I am associated with the log4cplus.lib file and it finds it in order. log4cplus.lib is also 100% functional, I use it in another project without any problems. My initial intention was to compile my project as a static library and use it in another DLL that I am writing, but when I do this, I get the same linker errors in this other project ...

Edit # 2:

Functions that cause linker errors are static functions. Could this be part of the problem?

+6
c ++ dll visual-studio-2008-sp1 log4cplus
source share
7 answers

wilx is right. I have a problem with the same connection.

It took me almost a day to solve this problem.

I downloaded log4cplus-1.0.4 after I opened this project with visual studio 2010, compiled it, both a static and a dynamic library, without errors.

however, when I try to use this library, I received a link error, regardless of the static library or the dynamic library.

the reason is that by default this project uses a multibyte character, however my own project uses unicode, so to solve this problem, you just need to change one project encoding.

both unicode and both multibyte encodings.

and change one project encoding in visual studio 2010, see the following link.

How to disable Unicode in a VC ++ project?

+6
source share

When you create a static library, the library creator does not try to allow all the functions that you use (those that are in log2cplus.lib). These functions are enabled when creating an executable file that links to your static library.

When you create a dynamic library, the library creator (linker) tries to solve all the functions that you use. You must provide the linker with the log4cplus.lib library as soon as you create the dynamic library. You cannot wait until you create an executable file.

+4
source share

In any case, you need to link to the library.

The difference is that when linking statically - all functionality is associated with the library used.

With dynamic linking, you link to an import library that has the functionality to load and use functions from the dll, and you are missing this step. Typically, the import library has the same name as the DLL you are referencing.

Edit:

I saw that the missing character is not "__imp ..." This means that the header file is not configured for dynamic binding, probably because you have LOG4CPLUS_BUILD_DLL or log4cplus_EXPORTS or DLL_EXPORT not defined in the project where you include the Log4Cplus headers.

+2
source share

Are you really linking to log2cplus.lib file? If you compile as a static library, then refer to it through final.exe, and not to the static library - maybe this is the difference?

+1
source share

Have you used __declspec (dllimport) and __declspec (dllexport)?

They are not needed when linking static libraries, but are required for DLLs. A function must be declared for export (in a DLL), so users can use it outside (and therefore import it from a DLL).

Perhaps this may help:

Import into the application using __declspec (dllimport)

http://msdn.microsoft.com/en-us/library/8fskxacy%28VS.80%29.aspx

Best wishes

0
source share

If you are using a compiler such as MSVC, it is possible that it changed the project settings without knowing it when switching from lib to dll. You should double check that in DLL mode you are correctly linked to lib.

0
source share

There are two possibilities that I see for the linker error:

  • You compiled log4cplus.dll (and the corresponding log4cplus.lib import library) using the provided release build configuration, but now your application is compiled with the "Character Set" option set to "Use Unicode Character Set"

  • Or you are trying to use the log4cplus static library, but defining LOG4CPLUS_BUILD_DLL for your application.

I put on number 1. It could be something else. What version of log4cplus are you using?

If you want to use log4cplus.dll in your application, define the symbol LOG4CPLUS_BUILD_DLL. log4cplus_EXPORTS and DLL_EXPORT exist only to support a build system based on CMake and a build system based on AutoTools on MingGW, respectively.

0
source share

All Articles