Having linker error "Unresolved External" - c++

I have code in two files like:
Static.cpp:
static int s_Variable = 5;
Main.cpp:
extern int s_Variable;
I get this error:
Build started...
1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
1>Static.cpp
1>Main.obj : error LNK2001: unresolved external symbol "int s_Variable" (?s_Variable##3HA)
1>E:\Visual Studio 2019\Dev\CPPSeries\HelloWorld\bin\Win32\Debug\HelloWorld.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "HelloWorld.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Everything compiles properly on each file, so I know it is just a linking issue. What exactly is wrong?

Static global variables do not have external linkage; in a sense, they are "local" to a given translation unit. You would have to use a non-static global variable if you wanted to refer to it in a separate translation unit.

Related

Constant linker errors when compiling a program using Dear ImGui

83
Back-ends: imgui_impl_dx9.cpp + imgui_impl_win32.cpp
Operating System: Windows 10
Compiler: Visual Studio 2019
My Issue/Question:
I keep getting constant linker errors when trying to compile my program and I cannot figure out what is causing them
I have the files required and they're included in the .sln properties and in my main.cpp
1>------ Build started: Project: ConsoleApplication2, Configuration: Debug Win32 ------
1>ConsoleApplication2.cpp
1>imgui_widgets.cpp
1>Generating Code...
1>ConsoleApplication2.obj : error LNK2019: unresolved external symbol _Direct3DCreate9#4 referenced in function "bool __cdecl CreateDeviceD3D(struct HWND__ *)" (?CreateDeviceD3D##YA_NPAUHWND__###Z)
1>imgui_impl_win32.obj : error LNK2019: unresolved external symbol _XInputGetState#8 referenced in function "void __cdecl ImGui_ImplWin32_UpdateGamepads(void)" (?ImGui_ImplWin32_UpdateGamepads##YAXXZ)
1>imgui_impl_win32.obj : error LNK2019: unresolved external symbol _XInputGetCapabilities#12 referenced in function "void __cdecl ImGui_ImplWin32_UpdateGamepads(void)" (?ImGui_ImplWin32_UpdateGamepads##YAXXZ)
1>C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x64\xinput.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86'
1>C:\Users\Logan\source\repos\ConsoleApplication2\Debug\ConsoleApplication2.exe : fatal error LNK1120: 3 unresolved externals
1>Done building project "ConsoleApplication2.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Visual studio 19 GLFW external symbol error

I use the GLFW example from https://www.glfw.org/documentation.html
I get these errors
Build started...
1>------ Build started: Project: generic RPG game, Configuration: Debug x64 ------
1>main.cpp
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>main.obj : error LNK2019: unresolved external symbol __imp_glClear referenced in function main
1>C:\Users\Joseph\Desktop\foldrr\generic RPG game\x64\Debug\generic RPG game.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "generic RPG game.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I included the glfw3.lib and the headers too
I use the x64 windows precompiled binaries version 3.3.2 (latest version as of right now)
if I comment out glClear(GL_COLOR_BUFFER_BIT); function in the example code the error goes away and I'm able to compile normally and by removing the GL_COLOR_BUFFER_BIT parameter in the function i get a 'glClear': function does not take 0 arguments error
also if i comment out the function and try to render simple triangle i get a error too
1>main.obj : error LNK2019: unresolved external symbol __imp_glBegin referenced in function main
1>main.obj : error LNK2019: unresolved external symbol __imp_glVertex2f referenced in function main
I know that similar question has been asked before but I didn't find anyone with this weird behaviour

I can't install libsndfile on visual studio

so I'm trying to write a program in Visual Studio (in C++) with use of libsndfile library, but I can't make it work. I have installed this library with vcpkg and when i try to run program from this site http://digitalsoundandmusic.com/5-3-3-reading-and-writing-formatted-audio-files-in-c/ I get 3 errors:
1>------ Build started: Project: ConsoleApplication4, Configuration: Debug Win32 ------
1>ConsoleApplication4.cpp
1>libsndfile.lib(flac.obj) : error LNK2001: unresolved external symbol _FLAC__StreamDecoderErrorStatusString
1>libsndfile.lib(flac.obj) : error LNK2001: unresolved external symbol _FLAC__StreamEncoderInitStatusString
1>C:\Users\asd\source\repos\ConsoleApplication4\Debug\ConsoleApplication4.exe : fatal error LNK1120: 2 unresolved externals
1>Done building project "ConsoleApplication4.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I tried also manually add this library but I had 68 errors similar to those above. Please, help me find solution. Thank you.

My simple introduction program to SDL does not build. Why am I getting these linker errors?

So, I'm trying to use SDL for the first time and so far it has been a complete mess! I'm trying to get this to run, but everything that I have tried has failed. I have been following all the correct guides but it still will not run!
I am using Visual Studio Ultimate 2010.
This is all the code that I am trying to run:
#include "SDL.h"
#include <iostream>
int main(int argc, char* args[]) {
// Start SDL
SDL_Init(SDL_INIT_EVERYTHING);
std::cout << "SDL has been initialized!\n";
// Quit SDL
SDL_Quit();
std::cout << "SDL has quit!" << std::endl;
return 0x0;
}
The errors that the compiler yaks back are driving me nuts! Here they are:
1>------ Build started: Project: SDL_TEST, Configuration: Debug Win32 ------
1>Build started 7/6/2013 1:21:45 PM.
1>InitializeBuildStatus:
1> Touching "Debug\SDL_TEST.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol _SDL_Quit referenced in function _SDL_main
1>main.obj : error LNK2019: unresolved external symbol _SDL_Init referenced in function _SDL_main
1>MSVCRT.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Tux\documents\visual studio 2010\Projects\SDL_TEST\Debug\SDL_TEST.exe : fatal error LNK1120: 3 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.59
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Compilation fails because you are not linking with the correct libraries. You have linked with the 64bit libraries but you are trying to build a Win32 binary:
1>------ Build started: Project: SDL_TEST, Configuration: Debug Win32 ------
Linking with the 32bit libraries or switching to a Win64 binary should resolve your problem.

Linking between projects

I have a solution with 2 projects in it:
The first one is called Engine and it has 3 classes: Backbuffer and Vector2 and CTimer. It is of type Console Application DLL, links winmm.lib, has all members in namespace "Engine" and if I build it (only this project) it compiles.
The other one, Game, links dependency for project Engine, is of type Win32 project, has additional include directories $(SolutionDir)\Engine\, has "Link Library Dependencies" set to true. And it does not compiles.
Both projects are compiled with "/MD" flag.
I don't know for sure if the problem is caused by VS12. Tried lots of solutions to similar errors but with no results for me.
The problem seems to be it doesn't compile the .CPP files. Thought if I compile only the "Engine" project, it compiles perfectly.
Errors:
1>------ Rebuild All started: Project: Engine, Configuration: Release Win32 ------
1> BackBuffer.cpp
1> CTimer.cpp
1> Vector2.cpp
1> Generating code
1> Finished generating code
1> Engine.vcxproj -> G:\Ubisoft\HumansVsZombies\Release\Engine.dll
2>------ Rebuild All started: Project: Game, Configuration: Release Win32 ------
2> CGameApp.cpp
2> CGameObject.cpp
2> Main.cpp
2>CGameApp.obj : error LNK2001: unresolved external symbol "public: void __thiscall Engine::BackBuffer::reset(void)" (?reset#BackBuffer#Engine##QAEXXZ)
2>CGameApp.obj : error LNK2001: unresolved external symbol "public: void __thiscall Engine::BackBuffer::present(void)" (?present#BackBuffer#Engine##QAEXXZ)
2>CGameApp.obj : error LNK2001: unresolved external symbol "public: __thiscall Engine::BackBuffer::~BackBuffer(void)" (??1BackBuffer#Engine##QAE#XZ)
....
2>G:\Ubisoft\HumansVsZombies\Release\Game.exe : fatal error LNK1120: 12 unresolved externals
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
Thank you !