Linking error in OpenGL using freeglut in CLion - c++

So, I am using freeglut to try to do some openGL stuff, but I keep getting errors saying that references are undefined:
CMakeFiles\texture_mapping.dir/objects.a(TextureMapper.cpp.obj): In function `ZN13TextureMapper4initEv':
.../TextureMapper.cpp:20: undefined reference to `glClearColor#16'
.../TextureMapper.cpp:23: undefined reference to `glMatrixMode#4'
.../TextureMapper.cpp:24: undefined reference to `glLoadIdentity#0'
.../TextureMapper.cpp:25: undefined reference to `glOrtho#48'
CMakeFiles\texture_mapping.dir/objects.a(TextureMapper.cpp.obj): In function `ZN13TextureMapper7displayEv':
.../TextureMapper.cpp:45: undefined reference to `glClear#4'
...TextureMapper.cpp:48: undefined reference to `glColor3f#12'
...TextureMapper.cpp:49: undefined reference to `glBegin#4'
...TextureMapper.cpp:52: undefined reference to `glVertex3f#12'
...TextureMapper.cpp:53: undefined reference to `glVertex3f#12'
...TextureMapper.cpp:54: undefined reference to `glVertex3f#12'
...TextureMapper.cpp:55: undefined reference to `glVertex3f#12'
...TextureMapper.cpp:58: undefined reference to `glEnd#0'
...TextureMapper.cpp:61: undefined reference to `glFlush#0'
I am using MinGW with CLion to do this project. I thought that I got everything correctly. I moved the appropriate files into the include folder in MinGW, as well as the bin folder, and also the lib folder. Then, I have this in my CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(texture_mapping)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp TextureMapper.cpp TextureMapper.h Vertex.h ObjParser.cpp ObjParser.h)
add_executable(texture_mapping ${SOURCE_FILES})
target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a)
The libraries I linked were the only library files that freeglut came with.
So, what am I missing? CLion doesn't show any errors before it is compiled. I can even go into the functions in the header files provided by freeglut. Why then, are these functions not defined in my program?

I have struggled with the same problem and solved it by appending following lines to CMakeLists.txt:
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})

You did not actually link OpenGL to your project, so you get undefined references to OpenGL functions. Try to replace
target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a)
with
target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a GL)
I reproduced your issue with your CMakeLists.txt and the following program:
#include <GL/gl.h>
int main() {
glClear(GL_COLOR_BUFFER_BIT);
return 0;
}
and solved it with the above replacement. The solution automatically links GL library from my library path:
$ ls -1 /usr/lib64/libGL.*
/usr/lib64/libGL.la
/usr/lib64/libGL.so
/usr/lib64/libGL.so.1
/usr/lib64/libGL.so.1.0.0
UPDATE
According to this, you have some variables to access your actual OpenGL libraries. For example, you may point to OpenGL library file(s) directly like this:
target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a ${OPENGL_gl_LIBRARY})
Also you may add OpenGL library directory to the library search path (do it before target_link_libraries):
link_directories(${OPENGL_gl_LIBRARY})

Reordering the CMakeLists.txt helped me (<name> should be replaced accordingly):
cmake_minimum_required(VERSION 3.10)
project(<name>)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lGL -lGLU -lglut")
set(CMAKE_CXX_STANDARD 17)
add_executable(<name> main.cpp)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})

Related

Getting tbb linker errors when including the <execution> header

I have been writing a visualisation tool using OpenGL. It has been compiling and linking just fine (using gcc 11.2.0) until I recently installed the Linux dependencies for OpenVDB (listed under Using UNIX apt-get). I am now getting linker errors that I have narrowed down to the inclusion of the <execution> header file:
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `tbb::detail::d1::execution_slot(tbb::detail::d1::execution_data const&)':
main.cpp:(.text._ZN3tbb6detail2d114execution_slotERKNS1_14execution_dataE[_ZN3tbb6detail2d114execution_slotERKNS1_14execution_dataE]+0x18): undefined reference to `tbb::detail::r1::execution_slot(tbb::detail::d1::execution_data const*)'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `tbb::detail::d1::current_thread_index()':
main.cpp:(.text._ZN3tbb6detail2d120current_thread_indexEv[_ZN3tbb6detail2d120current_thread_indexEv]+0x12): undefined reference to `tbb::detail::r1::execution_slot(tbb::detail::d1::execution_data const*)'
The above is from the following minimal example.
main.cpp:
#include <execution>
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(test)
set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)
add_executable(test main.cpp)
I have tried linking TBB with my project like so:
# Find and link TBB.
find_package(TBB REQUIRED)
include_directories(${TBB_INCLUDE_DIRS})
link_directories(${TBB_LIBRARY_DIRS})
add_definitions(${TBB_DEFINITIONS})
if(NOT TBB_FOUND)
message("Error: TBB not found")
endif(NOT TBB_FOUND)
add_executable(test main.cpp)
target_link_libraries(test ${TBB_LIBRARIES})
...and also adding the -ltbb linker flag (as per the answer to this post) using
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ltbb")
However, this does not solve the issue.
What I find particularly strange is that I didn't use to have to link against tbb despite having included the <excution> header all along. Only after installing the OpenVDB dependencies has this become an issue.
Can anyone advise me on how to solve this (either by appropriately linking tbb, or not having to link it at all, as was the case before)? Any form of insight would be much appreciated.
I have managed to successfully link tbb as follows:
find_package(TBB REQUIRED COMPONENTS tbb)
add_executable(test main.cpp)
target_link_libraries(test tbb)
Although I am still clueless as to why I suddenly needed to link with tbb, considering I previously didn't need to.
Uninstalling libtbb-dev also works and dont require editing the CMakeFiles.txt.

libVLC|make: undefined reference to `libvlc_new'

For a small project, I decided to use libvlc hence C/C++.
Using different references I somehow installed opencv and libvlc libraries and also wrote the following CMake file:
cmake_minimum_required(VERSION 3.10)
project(untitled1)
set(CMAKE_CXX_STANDARD 14)
SET(CMAKE_MODULE_PATH
${CMAKE_SOURCE_DIR}/cmake
${CMAKE_SOURCE_DIR}/config
${CMAKE_SOURCE_DIR}/config/platform
)
find_package(OpenCV REQUIRED)
find_package(LIBVLC REQUIRED)
file(GLOB SOURCE_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
)
add_executable(untitled1 ${SOURCE_FILES})
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(CMAKE_VERSION VERSION_LESS "2.8.11")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${LIBVLC_INCLUDE_DIRS})
set(LIBS ${LIBS} ${OpenCV_LIBS})
set(LIBS ${LIBS} ${LIBVLC_LIBRARIES} )
target_link_libraries( untitled1 ${LIBS})
But when I do, cmake and then make I get the following error:
[100%] Linking CXX executable untitled1
CMakeFiles/untitled1.dir/VLCReader.cpp.o: In function `VLCReader::VLCReader(char*)':
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:17: undefined reference to `libvlc_new'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:18: undefined reference to `libvlc_media_player_new'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:21: undefined reference to `libvlc_video_set_callbacks'
CMakeFiles/untitled1.dir/VLCReader.cpp.o: In function `VLCReader::~VLCReader()':
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:26: undefined reference to `libvlc_media_player_stop'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:27: undefined reference to `libvlc_media_player_release'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:28: undefined reference to `libvlc_release'
CMakeFiles/untitled1.dir/VLCReader.cpp.o: In function `VLCReader::start(int, int)':
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:33: undefined reference to `libvlc_media_player_pause'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:34: undefined reference to `libvlc_media_new_location'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:35: undefined reference to `libvlc_media_player_set_media'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:36: undefined reference to `libvlc_media_release'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:38: undefined reference to `libvlc_video_set_format'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:39: undefined reference to `libvlc_media_player_play'
CMakeFiles/untitled1.dir/VLCReader.cpp.o: In function `VLCReader::pause(bool)':
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:45: undefined reference to `libvlc_media_player_set_pause'
CMakeFiles/untitled1.dir/VLCReader.cpp.o: In function `VLCReader::updataSize()':
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:51: undefined reference to `libvlc_video_get_width'
/home/ravinder/CLionProjects/untitled1/VLCReader.cpp:52: undefined reference to `libvlc_video_get_height'
collect2: error: ld returned 1 exit status
CMakeFiles/untitled1.dir/build.make:166: recipe for target 'untitled1' failed
Please help me in going ahead. I am really new to C/C++ cmake based world.
Thanks #Tsyvarev and other people who pinted me in right direction.
My mistake in the CMake file is that I am using wrong variable names or say usng variables whihc are not defined.
LIBVLC_INCLUDE_DIRS to LIBVLC_INCLUDE_DIR
LIBVLC_LIBRARIES to LIBVLC_LIBRARY
Thanks all!

Cmake undefined reference to `boost::gregorian::greg_month::as_short_string() const'

I spend whole day trying to link the data_time library to my c++ cmake project.
So I am using cmake 3.4 and boost 1.61.0.
I have the class where is a function which is taking local time:
void TestClass::getTime() {
this->endDate = boost::posix_time::second_clock::local_time();
}
Then I want to return the value of endDate by function returning string:
string testCalss::Info() {
return to_simple_string(this->beginningDate);
}
I need to convert the type of endDate variable to string, because function is string type.
I am getting error messages while program is linking:
In function boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::simple_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::ostream&)':
/usr/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
/usr/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
I have read that data_time is not only header-only library and I should build and add it to my project.
I have tried this command gcc myapp.cpp -omyapp -lboost_date_time, and it's does not work because I am using g++ in cmake project and I haven't find nothing for g++.
I also tried this:
c++ -I path/to/boost_1_61_0 example.cpp -o example \
~/boost/stage/lib/libboost_regex-gcc34-mt-d-1_36.a
Its example from official boost docs how to link libraries.
In cmake project I have few cpp files. Should I run this command using file where is my converting command?
Is there any easier way to fix this error?
I found out how to solve the problem. The way I choose is simple. First of all I have used the locate */boost/date_time* command to find where is my boost and date_time libraries installed. I did not know what version of boost library I am using, so I used locate command again to find boost/version.hpp file. After that I added few lines to my CmakeLists.txt:
find_package( Boost 1.60.0 COMPONENTS date_time)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries( Program ${Boost_DATE_TIME_LIBRARY} )
endif()
Thats all.
cmake_minimum_required(VERSION 3.17)
project(boost_ptime)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES main.cpp)
add_executable(boost_ptime ${SOURCE_FILES})
find_package( Boost 1.73 COMPONENTS date_time)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries( boost_ptime ${Boost_DATE_TIME_LIBRARY} )
endif()
Thanks to Warszawski Koks, I was able to compile my program in this way. Above is the full CMakeLists.txt file.

How to link compiled cmph library in my project using CMake

In order to use cmph, a perfect minimal hashing library, in my project organised using CMake, I installed cmph library in a ubuntu machine and tested it using a single c file called main.c.
If I try to compile this file using gcc 5.3.0 using the following command:
gcc main.c
I will get the following output
/tmp/ccSOH5ob.o: In function `main':
testperfect.c:(.text+0x63): undefined reference to `cmph_io_vector_adapter'
testperfect.c:(.text+0x73): undefined reference to `cmph_config_new'
testperfect.c:(.text+0x88): undefined reference to `cmph_config_set_algo'
testperfect.c:(.text+0x9b): undefined reference to `cmph_config_set_mphf_fd'
testperfect.c:(.text+0xa7): undefined reference to `cmph_new'
testperfect.c:(.text+0xb7): undefined reference to `cmph_config_destroy'
testperfect.c:(.text+0xca): undefined reference to `cmph_dump'
testperfect.c:(.text+0xd6): undefined reference to `cmph_destroy'
testperfect.c:(.text+0xe2): undefined reference to `cmph_load'
testperfect.c:(.text+0x118): undefined reference to `cmph_search'
testperfect.c:(.text+0x153): undefined reference to `cmph_destroy'
testperfect.c:(.text+0x15f): undefined reference to `cmph_io_vector_adapter_destroy'
But if I run this command:
gcc main.c $(pkg-config --libs cmph) -o main
It will be compiled and run normally.
Now I need to add a similar piece of code in my project and the CMakeList.txt is written like this:
set(PROJECT_EXECUTABLE ${PROJECT_NAME})
# Compiling flags.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os")
endif()
# Inject project config.
configure_file(
${PROJECT_INCLUDE_DIR}/phsim/config.hpp.in
${PROJECT_INCLUDE_DIR}/phsim/config.hpp
)
include(FindPkgConfig)
find_package(PkgConfig REQUIRED)
pkg_search_module(CMPH REQUIRED cmph)
target_link_libraries(Phsim ${CMPH_LIBRARIES})
target_include_directories(Phsim PUBLIC ${CMPH_INCLUDE_DIRS})
target_compile_options(Phsim PUBLIC ${CMPH_CFLAGS_OTHER})
# Compile executable.
file(GLOB SOURCES ${PROJECT_SRC_DIR}/*.cpp)
add_executable(${PROJECT_EXECUTABLE} ${SOURCES})
set_target_properties(${PROJECT_EXECUTABLE} PROPERTIES
VERSION ${PHSIM_VERSION_LITER}
)
And then I try to run cmake . and make, but only get the error message:
CMake Error at src/CMakeLists.txt:20 (target_link_libraries):
Cannot specify link libraries for target "Phsim" which is not built by this
project.
But I won't get target executable file unless I compile the project. If I try to compile my project without those commands related to library linking, the compiler will give similar link errors provided in the beginning of my question.
I have checked the following questions:
Undefined reference to cmph functions even after installing cpmh library
And I tried instructions provided by these sites:
https://cmake.org/Wiki/CMake:How_To_Find_Libraries
https://cmake.org/cmake/help/v3.6/module/FindPkgConfig.html
Many thanks in advance.
Finally Solved.
cmake_minimum_required(VERSION 3.0.2)
project(TestGamma)
set(GAMMATEST_VERSION_MAJOR 1)
set(GAMMATEST_VERSION_MINOR 0)
set(CMPH_INCLUDE_DIR /usr/local/lib)
include(FindPkgConfig)
configure_file(
"${PROJECT_SOURCE_DIR}/TestGammaConfig.h.in"
"${PROJECT_BINARY_DIR}/TestGammaConfig.h"
)
include_directories(${PROJECT_BINARY_DIR})
set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(testgamma ${SOURCE_FILES})
pkg_check_modules(CMPH REQUIRED cmph)
include_directories(${CMPH_INDLUDE_DIR})
link_directories(${CMPH_INCLUDE_DIR})
target_link_libraries(testgamma cmph ${CMPH_INCLUDE_DIR})
Make sure to include pkgconfig at first and add link operations after calling "add_executable"
#usr1234567 Thank you for your attention.

libtiff4 error with ROS

I am trying to compile a ROS-package from a friend with catkin under Ubuntu 14.04 and am getting the following error:
/usr/bin/ld: warning: libboost_system.so.1.49.0, needed by
//usr/local/MATLAB/R2014a/bin/glnxa64/libut.so, may conflict with libboost_system.so.1.54.0
//usr/local/lib/libcvd.so: undefined reference to `TIFFWriteEncodedStrip#LIBTIFF_4.0'
//usr/local/lib/libcvd.so: undefined reference to `TIFFReadRGBAImageOriented#LIBTIFF_4.0'
//usr/local/lib/libcvd.so: undefined reference to `TIFFWriteScanline#LIBTIFF_4.0'
//usr/local/lib/libcvd.so: undefined reference to `TIFFGetField#LIBTIFF_4.0'
//usr/local/lib/libcvd.so: undefined reference to `TIFFClose#LIBTIFF_4.0'
//usr/local/lib/libcvd.so: undefined reference to `TIFFClientOpen#LIBTIFF_4.0'
//usr/local/lib/libcvd.so: undefined reference to `TIFFGetFieldDefaulted#LIBTIFF_4.0'
//usr/local/lib/libcvd.so: undefined reference to `TIFFStripSize#LIBTIFF_4.0'
//usr/local/lib/libcvd.so: undefined reference to `TIFFSetField#LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libharfbuzz.so.0: undefined reference to `FT_Face_GetCharVariantIndex'
//usr/local/lib/libcvd.so: undefined reference to `TIFFSetErrorHandler#LIBTIFF_4.0'
//usr/local/lib/libcvd.so: undefined reference to `TIFFReadScanline#LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libharfbuzz.so.0: undefined reference to `FT_Get_Advance'
collect2: error: ld returned 1 exit status
I have libcvd installed and also libtiff4-dev. Has anybody any idea, how to solve that issue?
Thanks a lot,
snow
EDIT: As suggest I include the CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(test)
set (test_VERSION "0.0.1")
find_package(OpenCV REQUIRED)
find_package(catkin REQUIRED COMPONENTS
test_core
cv_bridge
image_transport
roscpp
)
find_package(tracker)
set (CMAKE_CXX_FLAGS "-DNDEBUG -DNTIMING -DNRUN_UNIT_TESTS -g -O0 -std=c++11")
catkin_package(
INCLUDE_DIRS include
)
include_directories (include
${CMAKE_CURRENT_SOURCE_DIR}/include
${tracker_INCLUDE_DIRS}
${TRIANGULATION_INCLUDE_DIRS}
${OPENCV_INCLUDE_DIRS}
)
include_directories(/usr/local/MATLAB/R2014a/extern/include)
include_directories (SYSTEM
${catkin_INCLUDE_DIRS}
)
set (SOURCE
src/test/main.cc
src/test/rosbridge.cc
src/test/core.cc
)
add_executable (test ${SOURCE})
target_link_libraries(test
/lib/x86_64-linux-gnu/libssl.so.1.0.0
/lib/x86_64-linux-gnu/libcrypt.so.1
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0
/usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so
/usr/local/MATLAB/R2014a/bin/glnxa64/libeng.so
/usr/local/MATLAB/R2014a/bin/glnxa64/libmat.so
/usr/local/MATLAB/R2014a/bin/glnxa64/libut.so
${OpenCV_LIBS}
${tracker_LIBRARIES}
cvd
${catkin_LIBRARIES}
${TRIANGULATION_LIBRARIES}
)
Just linking cvd seems to not work in your case.
CMake comes with the great find_package feature, though, so let's use it:
Add find_package(CVD REQUIRED) at the top of the file
Add ${CVD_INCLUDE_DIRS} to include_directories
Replace cvd in target_link_libraries with ${CVD_LIBRARIES}
This may not work immediately but throw an error like "FindCVD.cmake not found". This is a script that searches your file system for the actual location of this library on your system and stores the paths to the variables used above. Many libraries already bring such an file themselves, but if this is not the case you have to provide it manually. In most cases you don't have to write this file yourself, though, as there is usually a bunch of open source projects, that already created such a file, which you can reuse (for example here). Just google "FindCVD.cmake" to find them.
Once you have this file:
Create a new subdirectory called "cmake" in your project and store the file there.
Add set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) at the top of your CMakeLists.txt (before the find_package call!)
Now it should hopefully work :)
I fixed it!
You have to link against the libtiff lib in your lib folder like this:
target_link_libraries(test
.
.
.
/usr/lib/x86_64-linux-gnu/libtiff.so.5
.
.
.
)