OpenGL hello.c fails to build using CMake - opengl

I am trying to build the hello.c example from http://www.glprogramming.com/red/chapter01.html (look for "Example 1-2").
My CMakeLists.txt is as follows:
cmake_minimum_required (VERSION 2.8)
project (GLUTEX)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIRS})
include_directories(${OpenGL_INCLUDE_DIRS})
add_executable (glutex glutex.c)
target_link_libraries (glutex ${OpenGL_LIBRARIES})
target_link_libraries (glutex ${GLUT_LIBRARIES})
The CMake call succeeds in generating the required Makefile. But when I call make, I encounter the following:
Scanning dependencies of target glutex
[100%] Building C object CMakeFiles/glutex.dir/glutex.c.o
Linking C executable glutex
/usr/bin/ld: CMakeFiles/glutex.dir/glutex.c.o: undefined reference to symbol 'glOrtho'
/usr/bin/ld: note: 'glOrtho' is defined in DSO /usr/lib64/libGL.so.1 so try adding it to the linker command line
/usr/lib64/libGL.so.1: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
make[2]: *** [glutex] Error 1
make[1]: *** [CMakeFiles/glutex.dir/all] Error 2
make: *** [all] Error 2
How do I fix this?

Try changing
target_link_libraries (glutex ${OpenGL_LIBRARIES})
to
target_link_libraries (glutex ${OPENGL_LIBRARIES})

Related

CMake & CodeSynthesis Header-only library not compiling

I have been trying to implement the runtime library which is a header-only library of CodeSynthesis. But I only get a linking compiling error whenever I try to run the generated files which are made by the XSD executable.
Here is the error to show you that I have a linking problem with the runtime library:
[100%] Linking CXX executable XercesGebeuren.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\XercesGebeuren.dir/objects.a(hello.cxx.obj): in function `xsd::cxx::xml::initialize()':
C:/Users/husey/Desktop/XercesGebeuren/xsd-4.0.0-i686-windows/libxsd/xsd/cxx/xml/elements.hxx:84: undefined reference to `xercesc_3_2::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_2::PanicHandler*, xercesc_3_2::MemoryManager*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\XercesGebeuren.dir/objects.a(hello.cxx.obj): in function `xsd::cxx::xml::terminate()':
C:/Users/husey/Desktop/XercesGebeuren/xsd-4.0.0-i686-windows/libxsd/xsd/cxx/xml/elements.hxx:90: undefined reference to `xercesc_3_2::XMLPlatformUtils::Terminate()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\XercesGebeuren.dir/objects.a(hello.cxx.obj): in function `xsd::cxx::xml::sax::std_input_stream::std_input_stream(std::istream&)':
C:/Users/husey/Desktop/XercesGebeuren/xsd-4.0.0-i686-windows/libxsd/xsd/cxx/xml/sax/std-input-source.hxx:27: undefined reference to `xercesc_3_2::BinInputStream::BinInputStream()'
---- [And so on] ----
collect2.exe: error: ld returned 1 exit status
mingw32-make[3]: *** [CMakeFiles\XercesGebeuren.dir\build.make:122: XercesGebeuren.exe] Error 1
mingw32-make[2]: *** [CMakeFiles\Makefile2:95: CMakeFiles/XercesGebeuren.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles\Makefile2:102: CMakeFiles/XercesGebeuren.dir/rule] Error 2
mingw32-make: *** [Makefile:137: XercesGebeuren] Error 2
I have followed the guide given by CodeSynthesis to generate my hello.cxx & hello.hxx files with the command: xsd cxx-tree --std c++11 hello.xsd
CMake
cmake_minimum_required(VERSION 3.17)
project(XercesGebeuren)
set(CMAKE_CXX_STANDARD 20)
set(Xsd_DIR ./cmake)
find_package(XercesC REQUIRED)
find_package(Xsd REQUIRED)
add_executable(XercesGebeuren main.cpp src/hello.cxx)
add_library(mylib INTERFACE)
target_include_directories(mylib PUBLIC INTERFACE "./libxsd/")
target_include_directories(XercesGebeuren PUBLIC ${XSD_INCLUDE_DIR})
target_link_libraries(XercesGebeuren PUBLIC mylib)
Thanks to 'Asteroids With Wings' I figured out that a header-only library should not be added as a library in CMake.
Including the library and linking the XercesC library was enough.
CMake solution
cmake_minimum_required(VERSION 3.17)
project(XercesGebeuren)
set(CMAKE_CXX_STANDARD 20)
set(Xsd_DIR ./cmake)
find_package(XercesC REQUIRED)
find_package(Xsd REQUIRED)
add_executable(XercesGebeuren main.cpp src/hello.cxx )
target_include_directories(XercesGebeuren PUBLIC ${XSD_INCLUDE_DIR})
target_link_libraries(XercesGebeuren PUBLIC XercesC::XercesC)

linker can't find libraries although defined in CMakeLists.txt

I'm quite new to work with cmake, and there is a problem I can't find the solution to:
I use the vtk library and specify this in the CMakeLists.txt file:
cmake_minimum_required (VERSION 2.6 FATAL_ERROR)
cmake_policy(SET CMP0053 OLD)
project (pcl-visualizer)
find_package (Qt4 REQUIRED)
find_package (VTK REQUIRED)
find_package (PCL 1.7.1 REQUIRED)
include_directories (${PCL_INCLUDE_DIRS})
link_directories (${PCL_LIBRARY_DIRS})
add_definitions (${PCL_DEFINITIONS})
set (project_SOURCES main.cpp pclviewer.cpp)
set (project_HEADERS pclviewer.h)
set (project_FORMS pclviewer.ui)
set (VTK_LIBRARIES vtkRendering vtkGraphics vtkHybrid QVTK)
QT4_WRAP_CPP (project_HEADERS_MOC ${project_HEADERS})
QT4_WRAP_UI (project_FORMS_HEADERS ${project_FORMS})
INCLUDE (${QT_USE_FILE})
ADD_DEFINITIONS (${QT_DEFINITIONS})
ADD_EXECUTABLE (pcl_visualizer ${project_SOURCES}
${project_FORMS_HEADERS}
${project_HEADERS_MOC})
TARGET_LINK_LIBRARIES (pcl_visualizer ${QT_LIBRARIES} ${PCL_LIBRARIES} ${VTK_LIBRARIES})
cmake also runs fine. It gives no error so I assume it can find all libraries, including the vtk library?
When I run make though, I get this error message:
[ 16%] Linking CXX executable pcl_visualizer
/usr/bin/ld: cannot find -lvtkRendering
/usr/bin/ld: cannot find -lvtkGraphics
/usr/bin/ld: cannot find -lvtkHybrid
/usr/bin/ld: cannot find -lQVTK
/usr/bin/ld: cannot find -lvtkRendering
/usr/bin/ld: cannot find -lvtkGraphics
/usr/bin/ld: cannot find -lvtkHybrid
/usr/bin/ld: cannot find -lQVTK
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/pcl_visualizer.dir/build.make:410: pcl_visualizer] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/pcl_visualizer.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I installed the vtk library via the package manager (pacman, I run arch linux), so it should be in one of the standard directories?
I checked if the right libraries are installed via pacman -Q vtk and it lists all needed libraries in the `/usr/lib/`` directory.

cmake link xlib directories c++

I'm trying to compile a c++ program that uses xlib with cmake. However, I'm having a problem including and linking xlib libraries in cmake file.
This is the error that I'm getting.
main.cpp:378: undefined reference to `XClearWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/project1.dir/build.make:94: recipe for target 'project1' failed
make[2]: *** [project1] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/project1.dir/all' failed
make[1]: *** [CMakeFiles/project1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
And when I use just the command line to compile, it works just fine.
I use this command (g++ main.cpp -L/usr/X11R6/lib -lX11)
and this is my cmake file.
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
link_directories(/usr/X11R6/lib)
include_directories(/usr/share/X11)
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
In your case, you forgot to specify the libraries that cmake should use to link your application (target_link_libraries or link_libraries).
But, why not just let cmake find the required path, libraries and includes by itself? I suggest you to use find_package(X11). In your case, you can try:
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_STANDARD 11) # for c++11
find_package(X11 REQUIRED)
link_libraries(${X11_LIBRARIES})
include_directories(${X11_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
I won't be able to explain why (if you have some idea, don't hesitate to comment my answer), but, in my case, I had to link X11 library using this command:
target_link_libraries( DisplayImage "-lX11" )
It works, at least, for the 3.5.1 version of cmake.

linking to a custom library using cmake

I have two cmake files that build a directory and then try to compile an example that uses that library. The content of the first CMakeLists.txt file stored at the project root ${CMAKE_SOURCE_DIR} is:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project("lsd_point_pair")
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
#Note: Eclipse automatically picks up include paths with this on!
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_CXX_FLAGS "-g")
find_package(PCL 1.7 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
set(BOOST_LIBS program_options)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
MESSAGE(" Boost_LIBRARIES: ${Boost_LIBRARIES}")
include_directories(${PROJECT_BINARY_DIR})
link_directories(${PROJECT_BINARY_DIR}/lib})
add_library(lsd_obj_rec_ransac lsd_obj_rec_ransac.cpp lsd_obj_rec_ransac.h lsd_point_pair_model_library.cpp lsd_point_pair_model_library.h ndim_voxel_structure.h orr_octree_cloud.h orr_octree_cloud.cpp)
target_link_libraries(lsd_obj_rec_ransac ${PCL_LIBRARIES} ${Boost_LIBRARIES})
######## subdirectories #########
add_subdirectory(examples)
In ${CMAKE_SOURCE_DIR}/examples I have the following CMakeLists.txt file:
add_executable(example_pcl_lsd_point_pair example_pcl_lsd_point_pair.cpp)
target_link_libraries(example_pcl_lsd_point_pair lsd_obj_rec_ransac ${Boost_LIBRARIES} ${PCL_LIBRARIES})
After successfully running cmake I run make and I get the linking errors:
CMakeFiles/example_pcl_lsd_point_pair.dir/example_pcl_lsd_point_pair.cpp.o: In function `~LSDObjRecRANSAC':
make[2]: Leaving directory `/media/Data/Documents/Grad/research/Projects/LSDPointPairs/qtcreator-build'
make[1]: Leaving directory `/media/Data/Documents/Grad/research/Projects/LSDPointPairs/qtcreator-build'
/home/mustafa/projects/LSDPointPairs/examples/../lsd_obj_rec_ransac.h:44: undefined reference to `pcl::recognition::ORROctreeCloud::~ORROctreeCloud()'
/home/mustafa/projects/LSDPointPairs/examples/../lsd_obj_rec_ransac.h:44: undefined reference to `pcl::recognition::ORROctreeCloud::~ORROctreeCloud()'
../lib/liblsd_obj_rec_ransac.a(lsd_obj_rec_ransac.cpp.o): In function `LSDObjRecRANSAC':
/home/mustafa/projects/LSDPointPairs/lsd_obj_rec_ransac.cpp:18: undefined reference to `pcl::recognition::LSDPointPairModelLibrary::LSDPointPairModelLibrary(float, float, float)'
/home/mustafa/projects/LSDPointPairs/lsd_obj_rec_ransac.cpp:18: undefined reference to `pcl::recognition::ORROctreeCloud::ORROctreeCloud()'
/home/mustafa/projects/LSDPointPairs/lsd_obj_rec_ransac.cpp:18: undefined reference to `pcl::recognition::ORROctreeCloud::~ORROctreeCloud()'
../lib/liblsd_obj_rec_ransac.a(lsd_point_pair_model_library.cpp.o): In function `LSDPointPairModel':
/home/mustafa/projects/LSDPointPairs/lsd_point_pair_model_library.h:36: undefined reference to `pcl::recognition::ORROctreeCloud::ORROctreeCloud()'
/home/mustafa/projects/LSDPointPairs/lsd_point_pair_model_library.h:36: undefined reference to `pcl::recognition::ORROctreeCloud::~ORROctreeCloud()'
../lib/liblsd_obj_rec_ransac.a(lsd_point_pair_model_library.cpp.o): In function `~LSDPointPairModel':
/home/mustafa/projects/LSDPointPairs/lsd_point_pair_model_library.h:27: undefined reference to `pcl::recognition::ORROctreeCloud::~ORROctreeCloud()'
/home/mustafa/projects/LSDPointPairs/lsd_point_pair_model_library.h:27: undefined reference to `pcl::recognition::ORROctreeCloud::~ORROctreeCloud()'
collect2: ld returned 1 exit status
make[2]: *** [bin/example_pcl_lsd_point_pair] Error 1
make[1]: *** [examples/CMakeFiles/example_pcl_lsd_point_pair.dir/all] Error 2
make: *** [all] Error 2
The library successfully gets built because I see the liblsd_obj_rec_ransac.a file gets generated. But the problem happens when trying to compile the example. What am I doing wrong?

Create a CMakeFiles.txt file for newbies

I have a project with the following structure
/cmake_modules/
FindSFML.cmake
/includes/
car.hpp
motor.hpp
tires.hpp
/sources/
car.cpp
motor.cpp
tires.cpp
/main.cpp
/main.hpp
I have the following CMakeFiles.txt file:
cmake_minimum_required(VERSION 2.8)
project (MYGAME)
set (MYGAME_VERSION_MAJOR 1)
set (MYGAME_VERSION_MINOR 0)
set (EXECUTABLE_NAME "mygame")
include_directories ("${MYGAME_BINARY_DIR}")
include_directories ("${MYGAME_BINARY_DIR}/includes")
link_directories ("${MYGAME_BINARY_DIR}/sources")
add_executable(${EXECUTABLE_NAME} main.cpp)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2.0 REQUIRED system window graphics network audio)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
When I try to execute make I get this:
[100%] Building CXX object CMakeFiles/mygame.dir/main.cpp.o
Linking CXX executable mygame
CMakeFiles/mygame.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x11): undefined reference to `mynamespace::Car::Instance()'
main.cpp:(.text+0x21): undefined reference to `mynamespace::Car::start()'
collect2: error: ld returned 1 exit status
make[2]: *** [mygame] Error 1
make[1]: *** [CMakeFiles/mygame.dir/all] Error 2
make: *** [all] Error 2
How do I fix it?
You need to include the rest of your sources (car.cpp, motor.cpp and tires.cpp) in the build in some way.
You can either add them along with main.cpp in the executable directly:
set(MySources sources/car.cpp sources/motor.cpp sources/tires.cpp main.cpp)
add_executable(${EXECUTABLE_NAME} ${MySources})
or you can make these into a library and link that:
set(MyLibSources sources/car.cpp sources/motor.cpp sources/tires.cpp)
add_library(MyLib ${MyLibSources})
add_executable(${EXECUTABLE_NAME} main.cpp)
...
target_link_libraries(${EXECUTABLE_NAME} MyLib ${SFML_LIBRARIES})
A couple of other points to note:
You should avoid the use of link_directories if possible (its own documentation discourages its use), and it's often helpful to include the headers in the list of files added via add_executable or add_library since these then show up in IDEs like MS Visual Studio.