Unresolved external symbol from a console application in visual studio - c++

I created a console application project(let's call it ProjectA) in visual studio, and another project which is the googletest project to test the ProjectA. Now I got unresolved external symbol problem when testing functions that have definition in cpp. (If I put everything in .h, there's no problem). How to make the content in the cpp visible to the googletest project? I notice ProjectA only creates a exe but no lib...
Many thanks!

As you have not provided code, I can give high level details.
Try to link your ProjectA to projectB So that when linker try to link it will find the proper definition.

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.

How to import code from .exe project for unit test

I use Visual Studio 2015 to develop C++ app.
I have solution with two projects: one is the .exe project and another - project to test code via Boost Unit Tests.
Everything compiled, but linker fails to build second project. It says error LNK2019: unresolved external symbol.
How to link second project to the code of the first project?
You cannot import code from exe (i.e. link against exe), at least not easily. You can create a third project with the common code, either static or dynamic library, and let the exe and the units test both depend and link against the library.
Another option would be to have everything in a single project, but that is more suitable for makefile or CMake project (and then have build targets "all" and "tests", for example).

Integrating two Projects of same Solution DLLs

I have one dll project (A) of plugin in visual studio solution. This means i have a solution with one dll project.
I want to make another project (B) which caters to some specific functionality of dll project. Final output is dll. So dll project (A) is my main project. new Project B uses some of the headers of the dll project (A).
How do i link them together ?
I tried adding references of A in B but that failed. When i just add header files in "Additional Include Directory" it gave linker errors because it could not find the cpp or definitions.
error LNK2019: unresolved external symbol
I also have problem in setting the output format for B. should it be .lib ?
Would "Add Existing Items" is the only solution ?

Finding Reasons for Linking Errors in Visual Studio

I'm in the process of porting one of my project to an existing framework I've been developing. As a result I have two project, one that's the original I developed and then the ported one.
The problem I'm having is that the new project doesn't link. The error is "... unresolved external symbol ..." where the symbols should be defined in a library that's linked together with the project. By using VS command line tool dumpbin.exe I confirmed that the symbols said to be missing are in the library. The original project works fine, only by removing the library from the Linker->Input->Additional Dependencies do I get the same errors for that project.
Are there other reasons for "... unresolved external symbol ..." or am I simply not linking the library?
Details
The library in question is OpenCL.lib and the errors are "error LNK2019: unresolved external symbol _clBuildProgram#24 referenced in function ..." for all OpenCL API calls. Using the technique described in Tools for inspecting .lib files? I get the symbols for each function as one would expect.
Okay, figured it out. The library I'm linking with is only for x64 architectures, switching to that platform got it working.
Would've liked a more descriptive error from VS but what can you do...

Linking DLL error visual studio

I walked through this tutorial and tried to recreate the example I get this error:
Error 1 error LNK2019: unresolved external symbol __imp__GetXyz#0 referenced in function _main app.obj app
If I download the source from the website and run the project in visual studio it works, but If I even copy the code from the original project in my own visual studio solution it still doesn't work. I guess it must be some project settings, I don't know. What do do?
You need to add the XyzLibrary.lib to your Linker settings within project settings.
You can also do it directly in code by using #pragma comment(lib,"xyzlibrary.lib") if you are using VisualStudio.
The reason why it works in the given sample without these 2 approaches is that in the solution file, the dependencies are set from XyzExe to XyzLib, and XyzProject has 'Link Library Dependencies' in Linker settings set to true. However, I am not too keen on this approach, as I don't like to store data important for building inside solution files (for larger projects, developer's solution files can differ from build system solution files)
Seems like you are missing some external library that needs to be linked in. Whatever provides the GetXyz function is missing.
Open the Solution that works, right click the project, Properties, Linker, Input, and check what their Additional Dependencies list up. You need to add the same to your own project.