opengl on ubuntu linux - Linker error: undefined references [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 just want to start in openGL programming, so I installed freeglut3 from the repositories of ubuntu and I have all mesa packages installed, as well as build-essentials.
Later, I copied one simple example of openGL wiki
In first, I tried to compile it with: gcc triangle.c -o triangle.o;
but, later, I read here: Linker error : undefined reference to symbol 'glOrtho' that i have to linked the libraries with -l.
So, I tried with gcc triangle.c -lglut -lGL -Wall -o triangle.o
Both return the same error:
/tmp/ccKLT4ac.o: In function `main':
triangle.c:(.text+0x2b): undefined reference to `glutInit'
triangle.c:(.text+0x37): undefined reference to `glutInitDisplayMode'
triangle.c:(.text+0x4b): undefined reference to `glutInitWindowSize'
triangle.c:(.text+0x57): undefined reference to `glutCreateWindow'
triangle.c:(.text+0x5c): undefined reference to `glewInit'
triangle.c:(.text+0x73): undefined reference to `glewGetErrorString'
triangle.c:(.text+0xaa): undefined reference to `glutDisplayFunc'
triangle.c:(.text+0xaf): undefined reference to `glutMainLoop'
collect2: error: ld returned 1 exit status
I've tried to include gl.h & glu.h libraries, try with a lot of different example programs in cpp,... and a lot of combinations. All return similar errors.
ah, in /usr/include I have all the libraries:
akronix#ASJ-Netbook:~$ ls /usr/include/GL
freeglut_ext.h glext.h glu_mangle.h glx.h glxproto.h
freeglut.h gl.h glut.h glxint.h glxtokens.h
freeglut_std.h gl_mangle.h glxew.h glx_mangle.h internal
glew.h glu.h glxext.h glxmd.h wglew.h

Try to compile by placing libraries at the last
gcc triangle.c -Wall -o triangle -lglut -lGL

Related

Why is function reference still unresolved when link library was found? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 5 months ago.
I am trying to implement my own thumbnail provider by following Microsoft's thumbnail provider code sample. The code has been coming together nicely, but now I needed to figure out a way to compile (on Windows). MSYS2 appears to be a popular option.
g++ -shared -o GrmgThumbnailProvider.dll -lshlwapi -lgdi32 Dll.cpp GrmgThumbnailProvider.cpp
This causes errors:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Zyl\AppData\Local\Temp\cc5HnXOH.o:Dll.cpp:(.text$_ZN13CClassFactory14QueryInterfaceERK5_GUIDPPv[_ZN13CClassFactory14QueryInterfaceERK5_GUIDPPv]+0x31): undefined reference to `QISearch'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Zyl\AppData\Local\Temp\ccXzQLhd.o:GrmgThumbnailProvider.cpp:(.text+0x36f): undefined reference to `__imp_CreateDIBSection'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Zyl\AppData\Local\Temp\ccXzQLhd.o:GrmgThumbnailProvider.cpp:(.text$_ZN18CGrmgThumbProvider14QueryInterfaceERK5_GUIDPPv[_ZN18CGrmgThumbProvider14QueryInterfaceERK5_GUIDPPv]+0x31): undefined reference to `QISearch'
collect2.exe: error: ld returned 1 exit status
The libraries are definitely found: g++ complains if I change the library names in the -l argument to some nonsense. How is it these references cannot be resolved despite the required libraries being both present and found?
QISearch
CreateDIBSection

bash: ./main: cannot execute binary file: Exec format error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I know this Question has been asked before, but none of the answers helped in my case.
I have Ubuntu 18.04 LTS installed and when I try to run my C++ program, I get this Error:
bash: ./main: cannot execute binary file: Exec format error
I know this error occurs, when a 32-bit program is tried to be run on a 64-bit machine. But using file main in the terminal outputs
main: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
and using uname -a outputs
Linux florian 4.15.0-46-generic #49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
What do I overlook?
Thanks in advance
EDIT:
I compile the program using
g++ -Wall -c -std=c++14 -o main main.cpp
This is how the folder with the files looks like:
https://www.bilder-upload.eu/bild-cf6106-1553082433.png.html
The Game.h is included in the main.cpp, thats why i use the -c flag
Trying to compile the file without the -c flag gives me this terminal output:
/tmp/ccBqZfJw.o: In function `main':
main.cpp:(.text+0x63): undefined reference to `Sep::Game::Game()'
main.cpp:(.text+0xaf): undefined reference to `Sep::Game::loadConfig(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0xec): undefined reference to `Sep::Game::printMap()'
main.cpp:(.text+0x102): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x121): undefined reference to `Sep::Game::printMap()'
main.cpp:(.text+0x13c): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x152): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x17b): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x19a): undefined reference to `Sep::Game::printMap()'
main.cpp:(.text+0x1b5): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x1d0): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x1eb): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x1f7): undefined reference to `Sep::Game::printMap()'
main.cpp:(.text+0x20d): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x233): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x25e): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x26f): undefined reference to `Sep::Game::~Game()'
main.cpp:(.text+0x2b9): undefined reference to `Sep::Game::~Game()'
collect2: error: ld returned 1 exit status
EDIT:
After trying to compile it withe the other files at the ame time it seems like we're getting closer to a solution, but there are still some errors:
Command:
g++ -Wall -std=c++14 -o main main.cpp Field.cpp Game.cpp
Output:
/tmp/ccrCypxF.o: In function `main':
main.cpp:(.text+0x26f): undefined reference to `Sep::Game::~Game()'
main.cpp:(.text+0x2b9): undefined reference to `Sep::Game::~Game()'
/tmp/ccxLZpfi.o: In function `Sep::Game::Game()':
Game.cpp:(.text+0x1f): undefined reference to `vtable for Sep::Game'
/tmp/ccxLZpfi.o: In function `Sep::Game::printMap()':
Game.cpp:(.text+0x104e): undefined reference to `Sep::Field::Field()'
/tmp/ccxLZpfi.o: In function `Sep::Game::move(int, int, int)':
Game.cpp:(.text+0x133b): undefined reference to `Sep::Field::Field()'
collect2: error: ld returned 1 exit status
You should compile all files in the same command, so that they can be linked into an executable:
g++ -Wall -std=c++14 -o main main.cpp Field.cpp Game.cpp
Hi and welcome here on SO.
You should explain how you compile and link your program, as I'm afraid there's something wrong there.
The problem is your file is not an executable. As the file utility states, it is a relocatable file, not an executable.
To learn more about the difference between relocatables and executables you can have a look at this answer here on SO: What is the difference between executable and relocatable in elf format?
EDIT
Since the OP has provided more details, here is the solution.
1) Drop the -c flag. As others have stated, that is plain wrong there;
2) Add all the .cpp files in your compile command:
g++ -Wall -std=c++14 -o main main.cpp Field.cpp Game.cpp

c++ tidy linker error

I am new to c++ and I have the following problem:
files:
- main.cpp
- utils.h
- utils.cpp
When I am doing:
g++ -c -std=c++11 utils.cpp (compiles)
g++ -c -std=c++11 main.cpp (compiles)
when I try to link:
g++ -o main.o utils.o
/usr/lib/gcc/i686-redhat-linux/4.8.3/../../../crt1.o: In function _start':
(.text+0x18): undefined reference tomain'
utils.o: In function clean_html(std::string const&)':
utils.cpp:(.text+0xfa): undefined reference totidyCreate'
utils.cpp:(.text+0x118): undefined reference to tidyOptSetBool'
utils.cpp:(.text+0x13b): undefined reference totidyOptSetBool'
utils.cpp:(.text+0x15e): undefined reference to tidyOptSetBool'
utils.cpp:(.text+0x181): undefined reference totidyOptSetBool'
utils.cpp:(.text+0x1a4): undefined reference to tidyOptSetBool'
utils.cpp:(.text+0x1c7): undefined reference totidyOptSetValue'
utils.cpp:(.text+0x1ea): undefined reference to tidyOptSetValue'
utils.cpp:(.text+0x209): undefined reference totidyOptSetBool'
utils.cpp:(.text+0x228): undefined reference to tidyOptSetBool'
utils.cpp:(.text+0x247): undefined reference totidyOptSetInt'
utils.cpp:(.text+0x281): undefined reference to tidyParseString'
utils.cpp:(.text+0x295): undefined reference totidyCleanAndRepair'
utils.cpp:(.text+0x2b0): undefined reference to tidySaveBuffer'
utils.cpp:(.text+0x322): undefined reference totidyBufFree'
utils.cpp:(.text+0x32d): undefined reference to `tidyRelease'
collect2: error: ld returned 1 exit status
In utils.cpp I have a function clean_html. When I remove this function the code is linked with success.
I am using gcc version 4.8.3 20140911 (Red Hat 4.8.3-7) (GCC) .
Tidy is installed via fedora repos using:
Package libtidy-devel-0.99.0-28.20091203.fc19.i686 already installed and latest version
Package libtidy-0.99.0-28.20091203.fc19.i686 already installed and latest version
Edit:
Forgot to mention:
- I include tidy.h using
#include
tidy.h is at /usr/include/tidy.h
You need to add -ltidy to your link command.

collect2.exe [Error] ld returned 1 exit status [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 8 years ago.
I am programming since a few days and I simply want to open a window with opengl. I got glew and glfw, I linked the library of glew and the headers of both but when I compile I get an error. I've got windows7. I use def c++.
This is the code:
#define GLEW_NO_GLU
#define GLFW_NO_GLU
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main() {
GLFWwindow* window1 = glfwCreateWindow(640, 480, "window1", NULL, NULL);
glfwMakeContextCurrent(window1);
return 0;
}
And this are the errors:
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\main.o In function `main':
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\main.cpp undefined reference to `glfwCreateWindow'
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\main.cpp undefined reference to `glfwMakeContextCurrent'
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\collect2.exe [Error] ld returned 1 exit status
This is the compiler log or however it is called:
Compiler: TDM-GCC 4.7.1 32-bit Release
Building Makefile"C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\Makefile.win"
Führe make... aus
mingw32-make.exe -f "C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\Makefile.win" all
g++.exe -D__DEBUG__ main.o -o Projekt2.exe -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib32" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32" -static-libgcc glew-1.10.0/lib/Release/Win32/glew32.lib -m32 -g3
main.o: In function `main':
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL/main.cpp:9: undefined reference to `glfwCreateWindow'
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL/main.cpp:10: undefined reference to `glfwMakeContextCurrent'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe: *** [Projekt2.exe] Error 1
Compilation failed after 0,14 seconds with errors
From the log messages you posted, it seems that you haven't configured your Dev-C++ project properly to link against glfw:
g++.exe -D__DEBUG__ main.o -o Projekt2.exe -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib32" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32" -static-libgcc glew-1.10.0/lib/Release/Win32/glew32.lib -m32 -g3
The glfw library is missing, that is why you're getting an undefined reference to its functions. You should set up it as you did with glew32.
Also, the link to What is an undefined reference/unresolved external symbol error and how do I fix it?, provided in the comments to your question, is worth the read.

Is there a default path to library in ubuntu? [duplicate]

This question already has an answer here:
g++ and boost linker error on Ubuntu oneiric
(1 answer)
Closed 8 years ago.
folks!
I'm using ubuntu 12.04 and trying to compile a boost test program. I installed boost with
sudo apt-get install libboost-all-dev
an so, I don't know where i can find my libraries. I tried do compile that using
g++ -lboost_system -lboost_thread boost_test.cpp
thinking maybe to have some default path where ubuntu installed boost automatically, but it was wrong and I had the following:
/tmp/ccTKXzTR.o: In function
__static_initialization_and_destruction_0(int, int)':
boost_test.cpp:(.text+0xcc): undefined reference to
boost::system::generic_category()' boost_test.cpp:(.text+0xd8):
undefined reference to boost::system::generic_category()'
boost_test.cpp:(.text+0xe4): undefined reference to
boost::system::system_category()' /tmp/ccTKXzTR.o: In function
boost::system::error_code::error_code()':
boost_test.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17):
undefined reference toboost::system::system_category()'
/tmp/ccTKXzTR.o: In function
boost::asio::error::get_system_category()':
boost_test.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5):
undefined reference toboost::system::system_category()'
/tmp/ccTKXzTR.o: In function
boost::asio::detail::posix_tss_ptr_create(unsigned int&)':
boost_test.cpp:(.text._ZN5boost4asio6detail20posix_tss_ptr_createERj[_ZN5boost4asio6detail20posix_tss_ptr_createERj]+0x19):
undefined reference topthread_key_create' /tmp/ccTKXzTR.o: In
function
boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service,
boost::asio::detail::task_io_service::thread_info>::context>::~posix_tss_ptr()':
boost_test.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceENS4_11thread_infoEE7contextEED2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceENS4_11thread_infoEE7contextEED5Ev]+0x15): undefined reference topthread_key_delete' /tmp/ccTKXzTR.o: In
function
boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::strand_service::strand_impl,
unsigned char>::context>::~posix_tss_ptr()':
boost_test.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_14strand_service11strand_implEhE7contextEED2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_14strand_service11strand_implEhE7contextEED5Ev]+0x15):
undefined reference topthread_key_delete' collect2: error: ld
returned 1 exit status
Can someone help-me to figure out if my library is correctly installed and compile that?
I found out /usr/include/boost is the path to boost headers and /usr/lib contains all boost libraries i want to.