C++ project compiles as static library but not dynamic (Visual Studio) - c++

I'm a little new to c++ in visual studio, and I'm trying to compile a massive C++ project with Visual Studio. I've gone through and added all source and header files to my project and also updated all of the include paths in the project properties.
If I have the type of project set to "Static Library (.Lib)" the project will compile without error and I'll have a nice fatty .lib file.
If I change the project over to a "Dynamic Library (.dll)" The project no longer compiles and fails on linking errors.
Here's an example of one:
Error 27 error LNK2001: unresolved external symbol "char const * __cdecl Project::toString(enum Project::compMode)" (?toString#Project##$$FYAPBDW4compMode#1##Z) H:\repo.project\user\tool\component.obj tool
Any help or background on what might be happening here?

Check if you defined the following member function
char const* Project::toString(Project::compMode)
When you compile as a static library an unresolved symbol is not an error, because it can be resolved later when you link with other code.
You may have forgotten to add some .cpp file to your project.

Related

Include all source file for build

I have C++ project in my Visual Studio. Additionally I placed directory with source files in it in order to use them like library in my main project.
Everything works fine while code browsing. I can define includes of library headers and navigate to functions.
I got problem during project build. I got a lot of linking errors which tells that system can't link to functions from library.
Error LNK2019 unresolved external symbol lv_task_create referenced in function monitor_init testLVGL C:\cpp_test\testLVGL\monitor.obj 1
Error LNK2019 unresolved external symbol lv_tick_inc referenced in function "int __cdecl tick_thread(void *)" (?tick_thread##YAHPEAX#Z) testLVGL C:\cpp_test\testLVGL\testLVGL.obj 1
Error LNK2019 unresolved external symbol lv_task_handler referenced in function SDL_main testLVGL C:\cpp_test\testLVGL\testLVGL.obj 1
Looks like Visual C++ is even not trying to build library.
How to tell Visual C++ to include all library files located in subdirectory in order to build and link to project?
Visual Studio does not include all the files in a directory on its own. You need to explicitly include them (you dont have to do this if you add a file within the Solution Explorer or within Visual Studio). Else it wont be listed to the compiler.
To include them, go to the solution explorer and select "Show all files". This will show you all the files in the directory. Then you can see the unlisted files marked with a red icon. Right click it and hit "Include in project" and the red icon will disappear. After this, the file will be listed as one of the project's file.

OpenCV and Visual Studio 2015: Why is including the static library not working?

I'm trying to set up a VS2015 solution I have with OpenCV 3.0.0, and I'm trying to do so using static libraries only.
Header files: the OpenCV header files are included correctly and I can reference them in my source files without issue.
Libraries: the static library file that was created for me when I used CMake to build OpenCV (called "ippicvmt.lib") is included correctly, and loads without issue.
Using OpenCV in code: when I try to use OpenCV classes/functions in my solution, I get linker errors when building similar to
LNK2001: unresolved external symbol "public: virtual double __thiscall cv::VideoCapture::get(int)const
I can right-click and choose "Go to definition" on my use of this function and it opens videoio.hpp and shows the declaration of the virtual method. The linker error I see is likely caused by the inability for visual studio to find the actual implementation of the method in the static library I have included in my solution.
It is my understanding that the only .lib file I need to include is the one static library file. That is all I have included right now.
Does anybody know why VS can't find the implementations of this code in the OpenCV static library?
I fixed my issue by disabling the flag for building with shared libs in CMake, after that way more .lib files show up in the sharedlib folder in my install. I added each .lib file I wanted to use to my additional dependencies in my project properties and my project built.

How to use a library in other c++ code?

I'm trying to use a library in my code.
But as I'm new in c++, I couldn't do that without any errors.
First I copied library in my main file and then in my main code I just included .h files.
When I ran it, it gave me some errors like:
error LNK2019: unresolved external symbol "public: __cdecl library"
The library contains .cpp and .h files of functions.
I'm doing it in Visual Studio 2012.
What should I do to use this lib?
Assuming you are using this library, The documentation to make an installation on Visual Studio is located here.
If this is your first time with C++ then this is a very complex project to start with... Using a library is a complex process as every single one has a diferent installation method.

"Unresolved external symbol __except_handler4_common" in Visual Studio 2015

I'm compiling a legacy Visual Studio 6.0 C++ application in Visual Studio 2015 and I've run into this error and searching the net hasn't yielded any useful information.
LNK2019 unresolved external symbol __except_handler4_common referenced in function __except_handler4 (MSVCRT.lib)
I understand that somewhere in the code is referencing a method no longer(?) present in current versions of MSVCRT. Is there a workaround / compiler flag for this?
The error message is actually saying the the function __except_handler4, defined in MSVCRT.LIB, references the undefined symbol __except_handler4_common. So it's not your code that's making the this reference, it's Visual Studio 2015's code.
The symbol __except_handler4_common is defined in vcruntime.lib. This file should be automatically be linked in. I'm not sure why it wasn't. Did you select the static runtime library in the project options ("Multi-threaded (/MT)"), but then manually add MSVCRT.LIB (part of the dynamic C runtime libary)?
In your library project, check Properties -> C/C++ -> Code Generation -> Runtime Library
Chances are it's set to "Multi Threaded Debug DLL" (/MDd).
If that's the case then try changing it to "Multi Threaded Debug" (/MTd) and rebuild (that worked for me).
The reason for this error depends.
For me it was "libcmt.lib" and "libcmtd.lib" listed explicitly among linker inputs, rather than by selecting it from "Runtime Library" field in GUI.
For me, I was linking to the objects of a static project from a non-static unit test. I tried setting the unit test to static build, but then the compiler (VC++ 2015) got the error An internal error has occurred in the compiler. I ended up setting both the main project and the unit test project to "Use MFC in a Shared DLL", and then it worked.

visual studio 2010 express linking file in different projects

in visual studio 2010 express edition. i have a project (a static library) called prj1.
prj1 has codes to create an object obj1.
in another project prj2 which is a dynamic library i have code which intantiates the object by "new obj1" (the code of obj1 is in prj1).
now when i try to build prj2 i get linking errors:
1> when i try to right click on prj2->properties->frameworkk and references if i add prj1 as new reference i get a lot of link errors like MSVCRTD.lib(MSVCR100D.dll) : error LNK2005:
2> if i do not do step1 above and right click on the solution->properties->project dependencies and then choose prj1 to depend on the prj2. then i get just get a link error which complains that : error LNK2019: unresolved external symbol obj1... unresolved externals.
It would be very kind if someone can help me resolve this issue.
Thanks
Seems that your options that choose runtime library differ. One project for example links to "multi-threaded debug DLL" and other project to "multi threaded debug". When you try to link the projects to each other then you get multiply defined runtime library symbols.
Your static library must be found/added to your library directories, and dependency list, too.