Link error when trying to build a simple OpenGL program - opengl

This is the OpenGL code:
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("Hello,world!");
glutDisplayFunc(display);
glutMainLoop();
}
The error messages are:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/zh/workspace/OpenGL/CppApplication_1'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/cppapplication_1
make[2]: Entering directory `/home/zh/workspace/OpenGL/CppApplication_1'
mkdir -p dist/Debug/GNU-Linux-x86
g++ -lglut -lGLU -lGL -lGLEW -o dist/Debug/GNU-Linux-x86/cppapplication_1 build/Debug/GNU-Linux-x86/main.o -L/usr/lib/x86_64-linux-gnu -Wl,-rpath,/usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/libglut.so /usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGLEW.so /usr/lib/x86_64-linux-gnu/libGLEWmx.so
/usr/bin/ld: build/Debug/GNU-Linux-x86/main.o: undefined reference to symbol 'glClear'
/usr/lib/x86_64-linux-gnu/libGL.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/cppapplication_1] Error 1
make[2]: Leaving directory `/home/zh/workspace/OpenGL/CppApplication_1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/zh/workspace/OpenGL/CppApplication_1'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 124ms)
At first I had thought this is because the OpenGL version installed is too low, but glClear is available from OpenGL 1.0 and exists in all versions (see here). This is version information of the OpenGL on my system.
I built the freeglut 2.8.1 and glew 1.10.0 and have them installed in my system:
I included the path to the library and specified needed library in the environment:
Also, I have read related threads in repository: enter link description here, enter link description here, but they don't help.
I'm worn out and really can not find what I am missing to build such a simple OpenGL code. Could you please tell me how to troubleshoot this problem? Thank you.
The environment I am using are:
Ubuntu 14.04 (64 bit)+
NetBeans 8.0
A hint: when I commented the glClear out, the program can be built successfully.
Edit: For those who working on windows 7 and encountering similar linking problem (OpenGL function not found), Vishwanath gowda's answer in this thread may help.
Edit2: We know that Windows supports OpenGL poorly, if you are at the same time using Intel's entrance-level integrated graphics card for which intel's driver provides no additional support of OpenGL, you'll have to make a new Mesa's OpenGL libray according to the guide here. This can be done because OpenGL is independent of hardware so can be implemented solely by software (A book claims so). Be careful to use machine=x86_64 if you are working on 64bit Win7. You can check that by observing the output of dumpbin /headers youropengldll.dll|more. You can also check that the functionality of OpenGL on your windows system is enhanced after that using a software "OpenGL Extension Viewer".

I'm able to compile your example program on a 64-bit Ubuntu 14.04 system using this command:
g++ example.c -lGL -lGLU -lGLEW -lglut -o example
The order of the link options is important: libraries have to be specified after the object files that depend on them. From the GCC documentation for the -l option:
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
(Strangely, the program compiles without errors on my Debian systems even if I put the -l options first. But not on Ubuntu.)

OK, I've found the key: I should explicitly specify one more library file that contains needed OpenGL function: libGL.so, as the following image shows:
Adding a few more lines, this is the output of this simple OpenGL program:
I think this should be a NetBeans-specific problem, maybe not appear on other IDE like eclipse.
edit: Although the above approach works, the key is to set the link option in NetBeans. All we need to do is type "-lGL -lGLU -lGLEW -lglut" in the Project Properties -> Linker -> Additional Options, as the image below shows:
Thank Wyzard for pointing out this.

Related

How to properly configure Clang?

I've just compiled latest reps of LLVM, Clang and libc++. Now however I have no idea how to configure the environment to use them. I've added in $PATH the one to compiled binaries and have set the
$D_LIBRARY_PATH=$(llvm-config --libdir)
but anyway when I test run 'clang' with example file it uses some '/usr/bin/ld' linker which I have no idea what is it (as I've uninstalled 'g++' because thought it was the problem (before 'clang' used some linker from it) and I don't have any other C++ compilers).
So now how do I point out the right 'llvm-ld', libc++ include and library paths? I don't want to pass some complex arguments every-time. Perhaps I should set some environment variables.
I'm also using KDevelop with the same effect.
Don't judge me if this sounds stupid but it's my first time with Linux (have always used Windows before). I'm using latest 'OpenSUSE' dist.
Update - here is the output window of CodeLite using clang compiler:
/bin/sh -c 'make -j 2 -e -f Makefile'
----------Building project:[ ClangTest - Debug ]---------- make[1]: Entering directory
'/run/media/bs_ld/8688602a-296d-40e1-bd37-c90e69f45769/Workspace/CL_C++_WP/ClangTest'
clang++ -c
"/run/media/bs_ld/8688602a-296d-40e1-bd37-c90e69f45769/Workspace/CL_C++_WP/ClangTest/main.cpp"
-stdlib=libc++ -o ./Debug/main.cpp.o -I. -I/run/media/bs_ld/8688602a-296d-40e1-bd37-c90e69f45769/Build/include/c++/v1/
clang++ -o ./Debug/ClangTest #"ClangTest.txt" -L.
-L/run/media/bs_ld/8688602a-296d-40e1-bd37-c90e69f45769/Build/lib/ /usr/bin/ld: cannot find crtbegin.o: No such file or directory
/usr/bin/ld: cannot find -lstdc++ /usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lgcc clang-3.7: error: linker command failed
with exit code 1 (use -v to see invocation) ClangTest.mk:76: recipe
for target 'Debug/ClangTest' failed make[1]: * [Debug/ClangTest]
Error 1 make[1]: Leaving directory
'/run/media/bs_ld/8688602a-296d-40e1-bd37-c90e69f45769/Workspace/CL_C++_WP/ClangTest'
Makefile:4: recipe for target 'All' failed make: * [All] Error 2 0
errors, 0 warnings
You should be able to run make install with perhaps an optional DESTDIR=/...... so that it doesn't clobber your system files.
Since you're on OpenSUSE, you might as well use your distribution's build services, and install the SVN version of LLVM-Clang from here. You should be able to find libc++ and LLVM itself as well.
Otherwise, make install DESTDIR=/opt/llvm should work, and then you can add /opt/llvm/bin/ to PATH and use libc++ by adding this compile and link option: -stdlib=libc++. You'll need something like /opt/llvm/lib in LD_LIBRARY_PATH as well to find the libc++ so.
This should work pretty much out of the box, but I have only ever used my distribution's packages, not a self-built Clang to do this.
Note that Clang still uses your system linker, ld, and this is fine. Currently, LLVM does not yet provide a fully functional alternative to this program, but they are working on it.
EDIT: It seems you uninstalled too much: Clang also uses the GCC crtbegin and crtend object files. So just install GCC again along with glibc and its dev package.

Error linking with mingw compiler using Codelite, can't find lib folder

I'm trying to get started with SDL and trying to compile a 'hello world' starter app to check my configuration and I get this error:
C:/MinGW-4.8.1/bin/g++.exe -c "C:/Users/Me/Documents/Cpp_Projects/Demo_Graphics/main.cpp" -g -O0 -Wall -o ./Debug/main.cpp.o -I. -IC:/Users/Me/Documents/Cpp_Projects/SDL2/32bit/include
C:/MinGW-4.8.1/bin/g++.exe -o ./Debug/Demo_Graphics #"Demo_Graphics.txt" -L. -lC:/Users/Me/Documents/Cpp_Projects/SDL2/32bit/lib/
c:/mingw-4.8.1/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lC:/Users/Me/Documents/Cpp_Projects/SDL2/32bit/lib/
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[1]: *** [Debug/Demo_Graphics] Error 1
Demo_Graphics.mk:79: recipe for target 'Debug/Demo_Graphics' failed
mingw32-make.exe[1]: Leaving directory 'C:/Users/Me/Documents/Cpp_Projects/Demo_Graphics'
mingw32-make.exe: *** [All] Error 2
Makefile:4: recipe for target 'All' failed
1 errors, 0 warnings
The directory C:/Users/Me/Documents/Cpp_Projects/SDL2/32bit/lib/ certainly does exist and has the required SDL libraries in it, but I have no clue about compilers.
This is predefined code which should work so the problem is in set up somewhere?
The -l compiler flag is adding a library dependency. It does not specify a path in which to look for libraries (that is the -L flag), which appears to be what you are expecting.
In your error message:
-L. -lC:/Users/Me/Documents/Cpp_Projects/SDL2/32bit/lib/
You can see that the compiler is looking in your current working directory (.) for the library C:/Users/Me/Documents/Cpp_Projects/SDL2/32bit/lib/, but this is not a library -- this is the directory in which you're storing the SDL libraries.
In the CodeLite linker setup you want to change your library path to C:/Users/Me/Documents/Cpp_Projects/SDL2/32bit/lib/, or use
-LC:/Users/Me/Documents/Cpp_Projects/SDL2/32bit/lib/
as a build flag.
You also need to link to the SDL2 library, so you'll need to add -lSDL2 (possibly -lSDL2main as well, I can't remember the specifics of the SDL libraries) to your build/linker flags.

C++ Boost on linux via Netbeans remote developement: undefined reference to boost::filesystem::path::codecvt()

So I installed Netbeans on windows7 and configured the Ubuntu box with samba to share the developement directory where I have the sources and the boost library.
I compiled the boost library to be able to link to boost_filesystem but I still get this error when I try to compile the application:
Copying project files to /home/nms/.netbeans/remote/ophelia.tele2.net/t2003915-Windows-x86_64/ at nms#ophelia.tele2.net
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/nms/.netbeans/remote/ophelia.tele2.net/t2003915-Windows-x86_64/Z/mr_deamon'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/mr_deamon
make[2]: Entering directory `/home/nms/.netbeans/remote/ophelia.tele2.net/t2003915- Windows-x86_64/Z/mr_deamon'
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/mr_deamon.o.d"
g++ -c -g -I/opt/mail-relay/mr_deamon/boost_1_55_0 -I/opt/mail-relay/mr_deamon/mysql_connector_cpp/include -MMD -MP -MF "build/Debug/GNU-Linux-x86/mr_deamon.o.d" -o build/Debug/GNU-Linux-x86/mr_deamon.o mr_deamon.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/mr_deamon build/Debug/GNU-Linux-x86/mr_deamon.o -L/opt/mail-relay/mr_deamon/mysql_connector_cpp/lib -L/opt/mail-relay/mr_deamon/boost_1_55_0/bin.v2/libs -lmysqlcppconn-static -lmysqlclient -lboost_system -lboost_filesystem
build/Debug/GNU-Linux-x86/mr_deamon.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)':
/opt/mail-relay/mr_deamon/boost_1_55_0/boost/filesystem/operations.hpp:447: undefined reference to `boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)'
build/Debug/GNU-Linux-x86/mr_deamon.o: In function `path<char*>':
/opt/mail-relay/mr_deamon/boost_1_55_0/boost/filesystem/path.hpp:139: undefined reference to `boost::filesystem::path::codecvt()'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/mr_deamon] Error 1
make[2]: Leaving directory `/home/nms/.netbeans/remote/ophelia.tele2.net/t2003915-Windows-x86_64/Z/mr_deamon'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/nms/.netbeans/remote/ophelia.tele2.net/t2003915-Windows-x86_64/Z/mr_deamon'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)
I thought I got the linking right.
Here is the program code:
#include <iostream>
#include <boost/filesystem.hpp>
//using namespace std;
using namespace boost::filesystem;
int main(int argc, char**argv)
{
std::cout << file_size(argv[0]);
return EXIT_SUCCESS;
}
The sql part works fine with linking and all, but only including the boost filesystem is enough to get a build error, can't get how to use remote developement with netbeans on windows to Linux. What am I doing wrong?
A new try without netbeans, just on Ubuntu with the example from Boost filesystem link ...
and with compile command:
g++ -pthread mr.cpp -lboost_filesystem-mt -lboost_system-mt
the output is
/tmp/ccIZaqeX.o: In function boost::filesystem::file_size(boost::filesystem::path const&)':
mr.cpp: (.text._ZN5boost10filesystem9file_sizeERKNS0_4pathE[boost::filesystem::file_size(boost::filesystem::path const&)]+0x19): undefined reference toboost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)'
/tmp/ccIZaqeX.o: In function boost::filesystem::path::path<char*>(char* const&, boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<char*>::type>, void>::type*)':
mr.cpp: (.text._ZN5boost10filesystem4pathC2IPcEERKT_PNS_9enable_ifINS0_11path_traits11is_pathableINS_5decayIS4_E4typeEEEvE4typeE[_ZN5boost10filesystem4pathC5IPcEERKT_PNS_9enable_ifINS0_11path_traits11is_pathableINS_5decayIS4_E4typeEEEvE4typeE]+0x22): undefined reference toboost::filesystem::path::codecvt()'
collect2: ld returned 1 exit status
I don't get it..
I got the same problem just yesterday:
How I solved:
First: if you installed Boost by source, make sure you are compiling it to right compiler toolset that you are using at netbeans. For instance, I tested it with MinGW # Windows 8 Boost/MingW.
Second: In Netbeans (I used 7.4 version), I added the libraries I want by hand:
Project Properties > Linker > Libraries > Add Library > Select the .a
files.
EDIT:
This link maybe be useful: undefined reference with boost when I try to compile
Great, so finally it worked, here is what I did:
1- Installed gcc 4.8 (not sure if this is relevant).
2- downloaded and unpacked boost into /usr/local
3- ran bootstrap.sh
4- modify toolset parameter in project-config.jam into using gcc : 4.8.1 : g++-4.8 ;
5- run ./b2 --with-system
6- run ./b2 --with-filesystem
both step 5 and 6 are done with user root.
7- compile the source file like this:
# g++-4.8 mr.cpp -L /usr/local/boost_1_55_0/stage/lib -I /usr/local/boost_1_55_0 -lboost_system -lboost_filesystem -o mr
Jee, finally, I was few minutes from quitting Boost.
What I think made a difference is the consistency of the compiler (toolset) and the install path, and specially those two "-L /usr/local/boost_1_55_0/stage/lib -I /usr/local/boost_1_55_0"

Compiling c++ with Armadillo library at NeatBeans

I am going to compile C++ program which contains Armadillo library.
This issue is feasible via command line with this command:
g++ '/arm.cpp' -o example -O1 -larmadillo
But when I add -O1 -larmadillo to the compile options of my NetBeans project I get a considerable amount of errors.
I got these errors:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/atx/NetBeansProjects/armadillo'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/armadillo
make[2]: Entering directory `/home/atx/NetBeansProjects/armadillo'
mkdir -p dist/Debug/GNU-Linux-x86
g++ -O3 -o dist/Debug/GNU-Linux-x86/armadillo build/Debug/GNU-Linux-x86/main.o
build/Debug/GNU-Linux-x86/main.o: In function `gemv<double>':
/usr/include/armadillo_bits/blas_wrapper.hpp:79: undefined reference to `wrapper_dgemv_'
/usr/include/armadillo_bits/blas_wrapper.hpp:79: undefined reference to `wrapper_dgemv_'
/usr/include/armadillo_bits/blas_wrapper.hpp:79: undefined reference to `wrapper_dgemv_'
/usr/include/armadillo_bits/blas_wrapper.hpp:79: undefined reference to `wrapper_dgemv_'
build/Debug/GNU-Linux-x86/main.o: In function `gemm<double>':
/usr/include/armadillo_bits/blas_wrapper.hpp:114: undefined reference to `wrapper_dgemm_'
/usr/include/armadillo_bits/blas_wrapper.hpp:114: undefined reference to `wrapper_dgemm_'
/usr/include/armadillo_bits/blas_wrapper.hpp:114: undefined reference to `wrapper_dgemm_'
/usr/include/armadillo_bits/blas_wrapper.hpp:114: undefined reference to `wrapper_dgemm_'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/armadillo] Error 1
make[2]: Leaving directory `/home/atx/NetBeansProjects/armadillo'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/atx/NetBeansProjects/armadillo'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 136ms)
Looks like your Armadillo installation is incomplete, or you have two versions of Armadillo installed. I recommend that you manually uninstall any previous versions of Armadillo (both the includes and the run-time library) and then do a fresh install, using a freshly downloaded Armadillo .tar.gz package: http://arma.sourceforge.net/download.html
Alternatively, you can work around the issue you're having. Edit "include/armadillo_bits/config.hpp" and comment out the line with ARMA_USE_WRAPPER. Then, instead of linking with -larmadillo, link with -lblas -llapack
I know this was an old question, but I had troubles recently with this so I want to help if someone else encounters same problems as I did. This is how-to setup Armadillo library in NetBeans C++ on 64 bit Windows 7.
Download the latest version of Armadillo from http://arma.sourceforge.net/download.html
Unpack it in some directory, where ever you want.
Go to Netbeans -> Project Properties -> C++ Compiler
3.1 Include Directories -> find your Armadillo directory and select folder "include".
3.2 Preprocessor Definitions -> ARMA_USE_LAPACK ARMA_USE_BLAS
Go to Netbeans -> Project Properties -> Linker
4.1 Additional Library Directories -> find your Armadillo directory and select folder "examples/lib_win64".
4.2 Additional Dependencies -> lapack_win64_MT.lib blas_win64_MT.lib
Go to your project's folder and add files from Armadillo/examples/lib_win64. 4 files should be added -> blas_win64_MT.dll, blas_win64_MT.lib, lapack_win64_MT.dll and lapack_win64_MT.lib.
This should be it. Now you can use Armadillo library in C++, just add in your cpp #include and if you want using namespace arma;
I hope this was helpful! Cheers!

ld lookup to find library

I'm cross compiling to an arm embedded system and receiving an error that I have questions about. Here is the error:
[ 19%] Built target cxjpeg-6b
Linking CXX shared library /home/botbear/JAUS++-2.110519- src/libraries/jaus++/2.0/lib/libcxutils.so
/home/botbear/openwrt/trunk/staging_dir/toolchain-arm_v6k_gcc-linaro_uClibc- 0.9.32_eabi/lib/gcc/arm-openwrt-linux-uclibcgnueabi/4.5.4/../../../../arm-openwrt-linux- uclibcgnueabi/bin/ld: cannot find -lpng
collect2: ld returned 1 exit status
make[2]: *** [/home/botbear/JAUS++-2.110519-src/libraries/jaus++/2.0/lib/libcxutils.so] Error 1
make[1]: *** [libcxutils/CxUtils/libcxutils/CMakeFiles/cxutils.dir/all] Error 2
make: *** [all] Error 2
As you can see the linker is looking for a static library named 'libpng'. In addition to the libpng lib., the command in the sub-make file using the -l switch to link to the following libraries: -lpng -lz -lX11 -lXtst -lpthread -Wl. All of the libraries (libpng,libz,libX11, etc) are in my host /usr/lib/ directory but my target toolchain does not have the the libraries. My question is if I manually recompile the libraries with my target compiler will that solved my problem? Or I'm going to have find and install packages for each of the libraries for my target platform.
Thanks in advance.
You seem to compile for the target, so you need target libraries. Either compiled by yourself or provided by the toolchain development environment (if you have one). The latter usually saves you some headaches (cross compiling and setting the paths where necessary).
I see you are using openwrt toolchain. Openwrt seems to have libpng as a package. I'd go with that if possible.