Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Project compiles as static lib, fails (linker error) as dynamic lib. why?

I've a VS2008 native C++ project, that I wish to compile as a DLL.

It only references one external library (log4cplus.lib), and uses its functions. (also uses log4cplus's .h files , naturally).

When I try to compile my project as a static library, it succeeeds. When I try as 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 just like this related to functions within log4cplus.lib.

It seems like something really stupid.. please help me :)

Thanks!

Edit :

I am linked against the log4cplus.lib file, and it finds it just fine. also, the log4cplus.lib is 100% functional, I am using it in another project with no problems. My original intention was to compile my project as a static library and use it in another DLL I am writing, but when do this, I get the same linker errors in that other project...

Edit #2 :

The functions which cause the linker errors are static functions.. could this be part of the problem?

like image 216
Roey Avatar asked May 17 '10 11:05

Roey


People also ask

What causes linker errors C++?

Linker errors occur when the linker is trying to put all the pieces of a program together to create an executable, and one or more pieces are missing. Typically, this can happen when an object file or libraries can't be found by the linker.

Can a DLL link to static library?

It is also necessary to link to the static library (defined previously in this topic) or to the import libraries for the PLUS DLL Interface. An import library tells your EXE how to connect to the PLUS DLL during runtime. Import libraries for Microsoft Visual C++ 6.0 SP6 or later compilers are provided.

Are static libraries linked?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.


1 Answers

wilx is right.I have the same link problem.

it took me almost one day to slove this issue.

I have downloaded log4cplus-1.0.4, after I opened this project with visual studio 2010,compile it, both static and dynamic library, no error.

however, when I try to use those library, I got a link error,no matter static library or dynamic library.

the reason is,by default, those project use multi-byte character, however, my own project use unicode,so,to slove those link problem, you just need to change one project's charset.

both unicode or both multi-byte charset.

and to change one project's charset in visual studio 2010, see the following link.

How do I turn off Unicode in a VC++ project?

like image 136
dape Avatar answered Oct 17 '22 12:10

dape