Compile GLFW code with g++ [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 7 years ago.
I am just starting to get into C++ coding, specifically OpenGL (GLFW at this stage). I am using MinGW, and compiling my code using a batch file:
g++ -time -v -I lib/glfw-3.1.2/include -o "bin/OpenGL Test" src/Main.cpp
My test code is this:
#include <iostream>
#include <GLFW/glfw3.h>
using namespace std;
int main() {
if(!glfwInit()) {
return 1;
}
glfwTerminate();
return 0;
}
To me this seems like it should compile and run correctly, but it throws errors at the two GLFW functions, claiming that they are 'undefined references'.
What have I done wrong?

'undefined references' is a common error. You should compile your cpp first. Then link it with the library to form executable file.

Related

Linker error while I try to add asio to my C++ project [duplicate]

This question already has answers here:
undefined reference to `__imp_WSACleanup'
(5 answers)
Closed last year.
I have a following simple program:
#include<iostream>
#include<boost/asio.hpp>
int main() {
return 0;
}
And when I try to compile it with (mingw) I get the following error:
C:\Users\Kamran\Desktop\Networking>g++ -I"C:\mingw\mingw\Include" -std=c++17 sou
rce.cpp
c:/mingw/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mi
ngw32/bin/ld.exe: C:\Users\Kamran\AppData\Local\Temp\cc4n0EJF.o:source.cpp:(.tex
t$_ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh[_ZN5boost4asio6
detail17winsock_init_base7startupERNS2_4dataEhh]+0x73): undefined reference to `
__imp_WSAStartup'
c:/mingw/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mi
ngw32/bin/ld.exe: C:\Users\Kamran\AppData\Local\Temp\cc4n0EJF.o:source.cpp:(.tex
t$_ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE[_ZN5boost4asio6de
tail17winsock_init_base7cleanupERNS2_4dataE]+0x35): undefined reference to `__im
p_WSACleanup'
Any idea how should I debug?
Assuming you have installed boost library on your Windows machine, you need to add -lwsock32 in your command line.

gcc can't find reference to function DoIt() when linking [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am currently becoming crazy. It seems like there is a problem with gcc and it can't open include files can't find the function DoIt() when linking. I tried compiling this code in code blocks and it didn't work so I tried it with G++ in the console and it still didn't work. So I think it's a problem with gcc.
This is my code
main.cpp
#include <iostream>
#include "source.h"
int main()
{
std::cout<<"That works"<<std::endl;
DoIt();
while(true)
{
}
return 0;
}
source.cpp
#include "source.h"
#include <iostream>
void DoIt()
{
std::cout<<"That works too"<<std::endl; //Currently doesn't work
}
source.h
void DoIt();
And this is what I wrote in the terminal
g++ main.cpp -std=c++11 -o result
This is the error message when i run it
/tmp/ccG6X4Bw.o: In function `main':
main.cpp:(.text+0x2d): undefined reference to `DoIt()'
collect2: error: ld returned 1 exit status
I have no clue why it doesn't work
By default, gcc will try to compile and link. Linking without the code from source.cpp will cause the linker to be unable to link the call of DoIt to its code. If you just wish to compile, pass -c to gcc.
From the man page:
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking.
...
-c Compile or assemble the source files, but do not link.
The linking stage simply is not done.

Functions of Open AL giving error of undefined reference while compiling with g++ in ubuntu [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I am completely new to Open AL.
So I started with installing Open AL library through command line
sudo apt-get install libopenal-dev
And also I installed alut installed with this command
sudo apt-get install libalut0 libalut-dev
Also I forked open Al from http://kcat.strangesoft.net/openal.html
and installed it also.
But when I am trying to compile this simple program:
#include <stdio.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alut.h>
int main(){
ALCdevice *device;
device=alcOpenDevice(NULL);
if(!device)
{
printf("no device found");
}
else
{
printf("device found");
}
return 0;
}
I am getting this error:
/tmp/cchMpaeS.o: In function main':
altest.cpp:(.text+0xe): undefined reference toalcOpenDevice'
collect2: error: ld returned 1 exit status
I compiled it with
g++ altest.cpp
g++ -std=c++11 altest.cpp
Both of them gave same error.
You need to link to the OpenAl library:
g++ -std=c++11 altest.cpp -lopenal

Cannot compile with boost? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
Attempting to create a simple WebSocket c++ application, the library I am using relies on boost.
I have attempted to add boost to my project but it refuses to compile, heres my cpp file
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
main()
{
printf("Hello World\n");
return 0;
}
I am attemting to compile it with g++ -I. -Iboost_1_58_0 /home/cabox/workspace/main.cpp -o /home/cabox/workspace/bin/exe, but all i get is
In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0xfba): undefined reference to`boost::system::generic_category()'
My folder layout is just my Main.cpp file, the boost_1_58_0 & the websocketpp folders all being in the same parent directory, what is it I'm doing wrong?
Add
-lboost_system
(see also What is an undefined reference/unresolved external symbol error and how do I fix it?)
Optionally specify the library directory with -L
Make sure it's after the cpps
Undefined reference to boost::system::generic_category despite linking with boost_system

Hash function SHA1 in C++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
I want to use SHA1 function from openssl library for hashing a string, I have downloaded the library and installed it in /usr/include, and here is my code:
#include <openssl/sha.h>
#include <string.h>
#include <stdio.h>
int main() {
unsigned char digest[SHA_DIGEST_LENGTH];
char string[] = "hello world";
SHA1((unsigned char*) &string, strlen(string), (unsigned char*) &digest);
}
It doesn't have any syntax error and it recognizes openssl/sha.h, but when I want to build the project in eclipse or build from the terminal, I get this error:
Hash.cpp:(.text+0x4a): undefined reference to `SHA1'
collect2: error: ld returned 1 exit status
Any help would be appreciated! :)
You didn't properly link openssl, if you're on Linux, you should link crypto.
From a terminal :
g++ -o hash hash.cpp -lcrypto
From eclipse, you should open project->Properties, go to C/C++ Build->Settings and add crypto in the Linker->Libraries folder.