Visual Studio 2012 C++ OpenGL GLFW linker error [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I am making a 2d side-scroller in C++ using OpenGL on Visual Studio 2010 Express. I am trying to compile my code, and it builds properly, but I get linker errors for the GLFW functions I am initializing in the main() function. Here is my code:
#include <iostream>
#include <ctime>
#include <GL\glfw.h>
#include "Player.h"
void render();
void update();
Player Player1;
//Cross platform sleep implementation
void _sleep(double ms)
{
double st = clock();
if(ms <= 0)
ms = 10;
while(clock() < (ms + st));
}
int main(int argc, char** argv)
{
std::cout <<"Loading Prized Fighter"<< std::endl;
glfwInit(); //Initialize GLFW
//Create GLFW window
if(glfwOpenWindow(800, 600, 5, 6, 5, 0, 8, 0, GLFW_WINDOW) != GL_TRUE)
std::cout << "Error creating window!" << std::endl;
//Set window title
glfwSetWindowTitle("Prized Fighter");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Start main game loop
//This calls the functions which update objects and render them to the screen
while(true)
{
update();
render();
glfwSwapBuffers(); //Switch buffers (double rendering)
_sleep(10.0); //Let a bit of CPU time for other processes
}
return 0;
}
/*
- Render function -
Used to draw objects to the screen
*/
void render()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Color to clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//TODO: Draw stuff here
Player1.draw();
}
/*
- Update function -
Updates objects; states, locations, etc
*/
void update()
{
//TODO: Update objects here
Player1.update();
}
When I compile, I get the following errors:
1>------ Build started: Project: Prized Fighter, Configuration: Debug Win32 ------
1>main.obj : error LNK2019: unresolved external symbol _glfwSwapBuffers referenced in >function _WinMain#8
1>main.obj : error LNK2019: unresolved external symbol _glfwSetWindowTitle referenced in >function _WinMain#8
1>main.obj : error LNK2019: unresolved external symbol _glfwOpenWindow referenced in >function _WinMain#8
1>main.obj : error LNK2019: unresolved external symbol _glfwInit referenced in function >_WinMain#8
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol WinMain#16 >referenced in function __tmainCRTStartup
1>c:\users\brennan\documents\visual studio 2010\Projects\Prized Fighter\Debug\Prized >Fighter.exe : fatal error LNK1120: 5 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What is going wrong during the linking - is there an error in the code?

It looks like you need to link against:
glfw.lib
opengl32.lib
It would be worth reading the release notes: here

You're getting these errors because you're not linking to the GFWL library, or not linking to it correctly. That library contains definitions for the functions you're trying to call.
Section 4.2 of the GFWL readme file explains which libraries to use when linking; if you're statically linking (looks like you are) or using the DLL, the steps you need will be different.

For future readers that already have linked against the correct libraries and might have a similar problem with GLAD files for C++ projects:
I simply had to include the glad.c file in the project via the Properties tab (Which should pop up whenever you click on a file, you can enable properties tab via View > Properties Window).
Make sure that "Included in project" is set to True:
This was the reason why I had unresolved external symbols for glad.c, which anyone in here might find useful.

I had everything linked in the settings correctly as far as I can tell but still for some reason visual studio 2017 was not including glad.c in my solution. Even though it is dropped into the folder it only appears if I select folder view, not solution view.
Ultimately I just typed #include "glad.c" after my pre-processor statements and it works for me.

Related

C++ Unit Test in Visual Studio 2019

I am having a problem with Visual Studio 2019 CPPUnitTestFramework. I follow the instructions, but every time I get an error. I have looked for tutorials, but anything I get is for Visual Studio 2017 and it does not solve my problem.
I am writing a program that uses OOP in C++ and I want to make unit tests, since it is going to be a considerably long project. The problem that I am having is that the program is not compiling in the test module.
Consider that I have the code such that I have the header file:
//A.h
#pragma once
class A
{
private:
// One parameter.
int a;
public:
// Add function.
int add(int, int);
// Subtract function.
int subtract(int, int);
A();
};
with the proper source file:
// A.cpp
#include "A.h"
int a;
int A::add(int alpha, int beta)
{
return alpha + beta;
}
int A::subtract(int alpha, int beta)
{
return alpha - beta;
}
A::A()
{
a = 4;
}
The structure of the program looks something like this:
To make my Unit Test, I right click on the "Solution 'TestTestUnit'" label and choose new project, look for the unit test, add the unit test and attach the reference, such that I get a file structure such as the one below:
To perform the unit test I write the code:
// TestUnitForTestTestUnit.cpp
#include "pch.h"
#include "CppUnitTest.h"
#include "../TestTestUnit/A.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace TestUnitForTestTestUnit
{
TEST_CLASS(TestUnitForTestTestUnit)
{
public:
TEST_METHOD(TestMethod1)
{
A first;
Assert::AreEqual(first.add(3, 2), 5);
}
};
}
When I try to run a test, the Test Explorer does nothing and throws the message: Aborting test run due to build failures. Please see the build for more details.
I cannot find the mistake here. The program runs perfect, but when instantiating a new "A" object the test fails. I am stuck here, are there any suggestions? What am I doing wrong (besides developing in Windows)?
UPDATE:
I have followed the suggestion to remove the namespace, as suggested by #ChrisMM, so that the test file now reads:
// TestUnitForTestTestUnit.cpp
#include "pch.h"
#include "CppUnitTest.h"
#include "../TestTestUnit/A.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
TEST_CLASS(TestUnitForTestTestUnit)
{
public:
TEST_METHOD(TestMethod1)
{
A first;
Assert::AreEqual(first.add(3, 2), 5);
}
};
such that when I run the Test Explorer gives the same message:
with error message:
1>------ Build started: Project: TestUnitForTestTestUnit, Configuration: Debug Win32 ------
1> Creating library C:\Users\<user>\Desktop\CPlusPlus\TestTestUnit\Debug\TestUnitForTestTestUnit.lib and object C:\Users\<user>\Desktop\CPlusPlus\TestTestUnit\Debug\TestUnitForTestTestUnit.exp
1>TestUnitForTestTestUnit.obj : error LNK2019: unresolved external symbol "public: int __thiscall A::add(int,int)" (?add#A##QAEHHH#Z) referenced in function "public: void __thiscall TestUnitForTestTestUnit::TestMethod1(void)" (?TestMethod1#TestUnitForTestTestUnit##QAEXXZ)
1>TestUnitForTestTestUnit.obj : error LNK2019: unresolved external symbol "public: __thiscall A::A(void)" (??0A##QAE#XZ) referenced in function "public: void __thiscall TestUnitForTestTestUnit::TestMethod1(void)" (?TestMethod1#TestUnitForTestTestUnit##QAEXXZ)
1>C:\Users\<user>\Desktop\CPlusPlus\TestTestUnit\Debug\TestUnitForTestTestUnit.dll : fatal error LNK1120: 2 unresolved externals
1>Done building project "TestUnitForTestTestUnit.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
Help would be appreciated.
You cannot put a test class inside a namespace. From the documentation
TEST_CLASS must be declared at namespace scope.
I suggest you to check if TestUnitForTestTestUnit has added Additional Dependencies. When I didn’t add it, the same problem as you occurred. After I added it, the program worked fine.
Right click TestUnitForTestTestUnit->Properties->C/C++->Linker->Input->Additional Dependencies-> add ..\TestTestUnit\Debug\*.obj

C++ DLL: unresolved external symbol

I seem to have some issues with creating new files for my project.
The issue is that in my sk_error.h file it seems to complain about unresolved external symbols (full error report below). When I place my OutOfRange class in my sk_interface.h file no one complains but when I put the class in the errors file it has issues with it.
If I was to comment out OutOfRange it works perfectly fine so I dont think that it is an issue with the DLL setup.
sk_error.h
#include <sk_platform.h>
#include <sk_interface.h>
namespace sky {
class SK_API OutOfRange : IError {
public:
OutOfRange() {
m_message = " Out Of Range";
m_value = (0 << 1);
}
std::string getMessage() override {
return m_message;
}
};
}
sk_platform.h
#if defined (SK_NONCLIENT_BUILD)
#ifndef SK_API
#define SK_API __declspec(dllexport)
#endif
#else
#ifndef SK_API
#define SK_API __declspec(dllimport)
#endif
#endif
sk_interface.h
#include <sk_platform.h>
#include <string>
namespace sky {
...
class SK_API IError {
public:
virtual std::string getMessage() = 0;
protected:
uint32_t m_value = 0;
std::string m_message = "Error not initialized";
};
}
The Client Project using the DLL
#include <sk_logmanager.h>
#include <sk_error.h>
#include <iostream>
int main() {
sky::g_LogManager.startup();
sky::OutOfRange err;
std::cout << err.getMessage() << "\n";
sky::g_LogManager.shutdown();
while (1) {}
}
Error Output
1>------ Build started: Project: SkyTest, Configuration: Debug Win32 ------
1>main.cpp
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sky::OutOfRange::OutOfRange(void)" (__imp_??0OutOfRange#sky##QAE#XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall sky::OutOfRange::getMessage(void)" (__imp_?getMessage#OutOfRange#sky##UAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sky::OutOfRange::~OutOfRange(void)" (__imp_??1OutOfRange#sky##QAE#XZ) referenced in function _main
1>C:\Users\Matt\Documents\Game Development\DevEnv\SkyTest\Debug\SkyTest.exe : fatal error LNK1120: 3 unresolved externals
1>Done building project "SkyTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Edit:
I am using Visual Studio 2017 (could be the source of the error). The Client Project is using the .lib file.
An unresolved external is always a link error. It even has it in its name: LNK2019.
It is telling you it cannot find the implementation for sky::OutOfRange::OutOfRange()
You have it in a header somewhere and you've called it, but you have not linked to the library that implements it.
We have no way of telling you what library implements it or where it lives on your hard drive. You will have to consult the documentation for OutOfRange, the author of it, or yourself.
I can tell you that you will want to check:
right click the executable project->
properties->linker->general->additional library directories
properties->linker->input->additional dependencies
and make sure the path to the library that defines OutOfRange is in the former and the library name is in the latter.
EDIT: If the library itself has a header that imports it, as it appears from the code you posted, you just need to set up the additional directories part.
In the end, you have to consult the documentation for whatever library you are using or hit up their forums.
I am not sure but this may be related to your solution configuration and solution platform. It wasn't working for me, when I set my solution configuration to "Debug" and platform to "x64"; it started working after setting it to Release - x86

ERROR LNK2019:EXE using DLL with multiple headers and Sources for a Main Class composed of other classes

I was trying to build "AccountDllTest.cpp" to test the dll "Account.dll" in Visual Studio 2013 but continuously getting this Linker Error ..
1>------ Build started: Project: AccountDllTest, Configuration: Debug x64 ------
1>AccountDllTest.obj : error LNK2019: unresolved external symbol "public: __cdecl Date::Date(int,int,int)" (??0Date##QEAA#HHH#Z) referenced in function main
1>AccountDllTest.obj : error LNK2019: unresolved external symbol "public: int __cdecl Date::getMonth(void)" (?getMonth#Date##QEAAHXZ) referenced in function main
1>C:\Users\Soumyadeep\Documents\Projects\AccountDllTest\x64\Debug\AccountDllTest.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The "Account.dll" was also successfully built with Visual Studio 2013 with the following files
Interface.h
Account.h
Date.h
Time.h
Account.cpp
Date.cpp
Time.cpp
Here the "Interface.h" just defines the abstract class "IAccount" containing pure virtual functions and the "Account.h" defines the "Account" Class derived from the "IAccount" class. The "IAccount" and "Account" classes are composed of objects of "Date" and "Time" classes defined and implemented in "Date.h","Date.cpp","Time.h","Time.cpp" respectively.
The code of AccountDllTest.cpp is given below
#include<iostream>
#include<conio.h>
#include"Headers\Interface.h"
#include<Windows.h>
using namespace std;
typedef IAccount* (_cdecl *FactoryFunc)();
int main()
{
HINSTANCE dll_handle = LoadLibrary(TEXT("..\\Core\\Account.dll"));
if (!dll_handle)
{
cout << "\n Unable to load Dll!!" << endl;
}
else
{
cout << "\n Dll successfully loaded!!" << endl;
}
FactoryFunc Create = (FactoryFunc)GetProcAddress(dll_handle, "createAccount");
IAccount* AC_1 = Create();
AC_1->setAccountNo(1);
AC_1->credit(2300);
cout << "\n Balance after credit" << AC_1->getBalance() << endl;
AC_1->lockAccount();
AC_1->credit(2300);
Date a;
a = AC_1->getLockDate();
cout<<"The account was locked on " ;
a.print();
_getch();
return 0;
}
The code was running without hiccups before I decided to test the Date objects after this line
AC_1->credit(2300);
What am I missing?
Sorry for this long question, but would really appreciate any help...
You must reference the DLL from your project.
To do this, simply:
In Solutions Explorer, click on your project and then, on the menu bar, click "Project" and next "References.
In "Common Properties", click "Framework and references" and next "Add new reference"
On the "Projects" tab, select your dll-project and click "OK".
Also you need to modify the includes directory path.
In Solutions Explorer, right click on your project and click "Properties"
In "C/C++" node (under "Configuration Properties"), select "General"
In "Additional Include Directories", add the path of the location of the headers (.h) of your DLL and click "OK"
You can read the official documentation for creating ad using a DLL.
https://msdn.microsoft.com/en-us/library/ms235636.aspx
Note that you have to add the defines on your header:
#ifdef YOURHEADER_EXPORTS
#define YOURHEADER_API __declspec(dllexport)
#else
#define YOURHEADER_API __declspec(dllimport)
#endif
If you use classes from your dll (as I see this is the Date class), first of all such classes must be imported with __declspec dllimport and secondly you have to link your test program with your dll library (yourdll.lib) to resolve external symbols:
AccountDllTest.obj : error LNK2019: unresolved external symbol "public: __cdecl Date::Date(int,int,int)" (??0Date##QEAA#HHH#Z) referenced in function main
AccountDllTest.obj : error LNK2019: unresolved external symbol "public: int __cdecl Date::getMonth(void)" (?getMonth#Date##QEAAHXZ) referenced in function main
Try to read about dllimport and dllexport here: https://msdn.microsoft.com/en-us/library/81h27t8c.aspx

How to use of FLTK in-box.get_string()

The following code is a small part of an app. For that app I need to use of in_box.get_string(). The declaration and definition of in_box are in this address in GUI.h and GUI.cpp files.
For integers, it works fine. That is, if I use in_box.get_int() it gets the number entered to the in_box and returns that number as an integer. But the problem is that, it doesn't work for strings, as is expected. And when using strings, I get errors!
For example in code below I have used a string s.
#include <GUI.h>
#include <iostream>
using namespace Graph_lib;
class Calculator : public Window {
public:
Calculator(Point, int, int, const string&);
private:
//Widgets
Button show_button;
In_box enter_box;
void show() { getstr(); cout <<s; }
void getstr() {
s = enter_box.get_string();
}
static void cb_show(Address, Address pw) { reference_to<Calculator>(pw).show(); }
string s;
};
//---------------------------------------------------------------------
Calculator::Calculator(Point xy, int w, int h, const string& title):
Window(xy, w, h, title),
show_button(Point(x_max()-110, 440), 90, 35, "Show", cb_show),
enter_box(Point(x_max()-400, 158), 245, 40, "Enter"){
attach(enter_box);
attach(show_button);
}
int main()
{
Calculator cal(Point(100,100), 600, 500, "Calculator");
return gui_main();
}
There are 8 warnings and two errors. Errors are these:
Error 9 error LNK2019: unresolved external symbol "public: class std::basic_string,class
std::allocator > __thiscall Graph_lib::In_box::get_string(void)"
(?get_string#In_box#Graph_lib##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
referenced in function "private: void __thiscall
Calculator::getstr(void)"
(?getstr#Calculator##AAEXXZ) C:\Users\ME\documents\visual studio
2012\Projects\test_3\test_3\test_3.obj Error 10 error LNK1120: 1
unresolved externals C:\Users\ME\documents\visual studio
2012\Projects\test_3\Debug\test_3.exe
The errors say, that is a linker problem but my linker input's file list is as follows and it has worked (almost) fine so far.
fltkd.lib
wsock32.lib
comctl32.lib
fltkjpegd.lib
fltkimagesd.lib
My compiler is MS VS 2012. And machine is Win 7.
Does anyone know how to solve the problem please?
If you have compiled FLTK in Debug and Release mode, make sure you have the linker dependencies right: the files you list for Debug, and fltk.lib, wsock32.lib, comctl32.lib, fltkjpegd.lib and fltkimages.lib for Release (notice absence of "d" at the end of the names).
Secondly, even though I don't think it causes the linker error (but leads to other weird problems), make sure you've included all the Stroustrup graphic library files in your Visual Studio project: fltk.h, Graph.h, GUI.h, Point.h, Simple_window.h, std_lib_facilities.h, Window.h, Graph.cpp, GUI.cpp and Window.cpp.

error LNK2019: unresolved external symbol _luaJIT_setmode

I have this piece of code (file luascript.cpp):
bool LuaInterface::initState()
{
m_luaState = luaL_newstate();
if(!m_luaState)
return false;
luaL_openlibs(m_luaState);
#ifdef __LUAJIT__
luaJIT_setmode(m_luaState, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_ON);
#endif
registerFunctions();
if(!loadDirectory(getFilePath(FILE_TYPE_OTHER, "lib/"), false, true))
std::clog << "[Warning - LuaInterface::initState] Cannot load " << getFilePath(FILE_TYPE_OTHER, "lib/") << std::endl;
lua_newtable(m_luaState);
lua_setfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
m_runningEvent = EVENT_ID_USER;
return true;
}
the declaration (file luajit.h):
LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
and the error is:
1>luascript.obj : error LNK2019: unresolved external symbol _luaJIT_setmode referenced in function "public: virtual bool __thiscall LuaInterface::initState(void)" (?initState#LuaInterface##UAE_NXZ)
1>C:\Users\GUIAKI\Documents\trunk.r5918\vc10\Debug\tfs.exe : fatal error LNK1120: 1 unresolved externals
How can I solve it?
Simply remove that line.
You can't link against plain Lua, if you keep it. And if you link against LuaJIT, the JIT compiler is enabled by default, anyway. That line of code is utterly pointless.
Seems like you forgot to link the library being part of "luaJIT" (never heard of it or used it).
There should be a lib file you'll have to add to your project as an additional dependency (linker settings).
Also keep in mind to include the correct headers ("lua.hpp" for C++, "luajit.h" for C).