error linking GMP library - c++

I have the following CMakeList.txt
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
find_package(GMP REQUIRED)
add_executable(Turrial main.cpp)
with the following simple source file main.cpp
#include <iostream>
#include <gmp.h>
#include <cstdio>
using namespace std;
int main(){
mpz_t a,b;
mpz_init_set_str(a,"45",10);
mpz_init_set_str(b,"12",10);
mpz_add(a,a,b);
cout <<mpz_get_str(NULL,10,a)<<endl;
}
I have included a FindGMP.cmake file that i found online :
set(GMP_PREFIX "" CACHE PATH "path ")
find_path(GMP_INCLUDE_DIR gmp.h gmpxx.h
PATHS ${GMP_PREFIX}/include /usr/include /usr/local/include )
find_library(GMP_LIBRARY NAMES gmp libgmp
PATHS ${GMP_PREFIX}/lib /usr/lib /usr/local/lib)
if(GMP_INCLUDE_DIR AND GMP_LIBRARY)
get_filename_component(GMP_LIBRARY_DIR ${GMP_LIBRARY} PATH)
set(GMP_FOUND TRUE)
endif()
if(GMP_FOUND)
if(NOT GMP_FIND_QUIETLY)
MESSAGE(STATUS "Found GMP: ${GMP_LIBRARY}")
endif()
elseif(GMP_FOUND)
if(GMP_FIND_REQUIRED)
message(FATAL_ERROR "Could not find GMP")
endif()
endif(
when I run cmake I have the following output:
moyle#localhost:~/Desktop/gmptri$ cmake .
-- Found GMP: /usr/local/lib/libgmp.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/moyle/Desktop/gmptri
Then I run make and i get the following errors :
(precise)moyle#localhost:~/Desktop/gmptri$ make VERBOSE=1
/usr/bin/cmake -H/home/moyle/Desktop/gmptri -B/home/moyle/Desktop/gmptri --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/moyle/Desktop/gmptri/CMakeFiles /home/moyle/Desktop/gmptri/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/moyle/Desktop/gmptri'
make -f CMakeFiles/Turrial.dir/build.make CMakeFiles/Turrial.dir/depend
make[2]: Entering directory `/home/moyle/Desktop/gmptri'
cd /home/moyle/Desktop/gmptri && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri/CMakeFiles/Turrial.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/moyle/Desktop/gmptri'
make -f CMakeFiles/Turrial.dir/build.make CMakeFiles/Turrial.dir/build
make[2]: Entering directory `/home/moyle/Desktop/gmptri'
Linking CXX executable Turrial
/usr/bin/cmake -E cmake_link_script CMakeFiles/Turrial.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/Turrial.dir/main.cpp.o -o Turrial -rdynamic
CMakeFiles/Turrial.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `__gmpz_init_set_str'
main.cpp:(.text+0x30): undefined reference to `__gmpz_init_set_str'
main.cpp:(.text+0x47): undefined reference to `__gmpz_add'
collect2: ld returned 1 exit status
make[2]: *** [Turrial] Error 1
make[2]: Leaving directory `/home/moyle/Desktop/gmptri'
make[1]: *** [CMakeFiles/Turrial.dir/all] Error 2
make[1]: Leaving directory `/home/moyle/Desktop/gmptri'
make: *** [all] Error 2
can anyone help me?

You forget to link libraries with your executable.
find_package(GMP REQUIRED)
add_executable(Turrial main.cpp)
target_link_libraries(Turrial gmp libgmp)

Related

Linking error on CMake and boost when compiling with MinGW on windows: undefined reference

I am trying to build a project on Windows using CMake and MinGW.
When using the command mingw32-make.exe I got link errors for any boost library (except for unit_test library), as for example:
"undefined reference to `boost::chrono::system_clock::now()'"
My boost installation is under C:\boost\ that contains both include and lib directories. I know that the boost installation is correct because if I use a handmade Makefile and I link the libraries with -l I can compile my project without any issue. So I guess the problem is related to how I link libraries in my CMakeLists.txt.
I tried these solution but they didn't work:
CMake Boost Static Libraries Undefined Reference boost::chrono::steady_clock::now()
Cmake with "undefined references" Despite Finding Boost Libs
Undefined reference to 'boost::system::generic_category()'?
This is the CMakeLists I am using, that correctly finds the libraries, as printed by Boost_DEBUG:
CMAKE_MINIMUM_REQUIRED(VERSION 3.23)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)
SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_DETAILED_FAILURE_MSG OFF)
SET(Boost_USE_MULTITHREADED ON)
SET(Boost_THREADAPI win32)
SET(BOOST_ROOT "C:\\boost")
SET(Boost_LIBRARY_DIR "C:\\boost\\lib")
SET(Boost_INCLUDE_DIR "C:/boost/include/boost-1_76")
SET(Boost_INCLUDE_DIRS "C:/boost/include/boost-1_76")
SET(Boost_USE_STATIC_RUNTIME OFF)
SET(Boost_DEBUG ON)
PROJECT(myproject LANGUAGES CXX)
find_package(Boost 1.76.0
COMPONENTS
log
system
timer
chrono
unit_test_framework
REQUIRED
)
add_executable(test_cmake test_cmake.cpp)
target_include_directories(test_cmake PRIVATE ${Boost_INCLUDE_DIRS})
target_include_directories(test_cmake PRIVATE ${Boost_LIBRARY_DIR})
target_link_libraries(test_cmake
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
${Boost_LIBRARIES}
)
This is the simple main file that can be compiled and produces "undefined reference to `boost::chrono::system_clock::now()'" error.
#define BOOST_AUTO_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/included/unit_test.hpp>
#include <turtle/mock.hpp>
// #include <boost/chrono.hpp> // I tried this too but it didn't work as well
#include <boost/chrono/chrono.hpp>
/**
* #brief Test of boost chrono library
*/
BOOST_AUTO_TEST_CASE(test_boost_chrono) {
auto start_time = boost::chrono::system_clock::now();
int sleep_time = 1000;
// boost::this_thread::sleep_for(boost::chrono::milliseconds(sleep_time));
auto curr_time = boost::chrono::system_clock::now();
auto diff_time = boost::chrono::duration_cast<boost::chrono::milliseconds>(curr_time - start_time);
BOOST_CHECK(diff_time.count() > sleep_time);
int epsilon = 10;
BOOST_CHECK(diff_time.count() < (sleep_time + epsilon));
}
I think the problem is related to target_include_directories and target_link_libraries but I cannot understand which is the issue.
Do you have any suggestion?
Edit:
This is what I get when compiling with mingw32-make.exe VERBOSE=1:
PS C:\Workspace\test_cmake_project_cmake\build> mingw32-make.exe VERBOSE=1
"C:\Program Files\CMake\bin\cmake.exe" -SC:\Workspace\test_cmake_project_cmake -BC:\Workspace\test_cmake_project_cmake\build --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start C:\Workspace\test_cmake_project_cmake\build\CMakeFiles C:\Workspace\test_cmake_project_cmake\build\\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make -f CMakeFiles\Makefile2 all
mingw32-make[1]: Entering directory 'C:/Workspace/test_cmake_project_cmake/build'
C:/MinGW/bin/mingw32-make -f test_cmake\CMakeFiles\test_cmake.dir\build.make test_cmake/CMakeFiles/test_cmake.dir/depend
mingw32-make[2]: Entering directory 'C:/Workspace/test_cmake_project_cmake/build'
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" C:\Workspace\test_cmake_project_cmake C:\Workspace\test_cmake_project_cmake\test_cmake C:\Workspace\test_cmake_project_cmake\build C:\Workspace\test_cmake_project_cmake\build\test_cmake C:\Workspace\test_cmake_project_cmake\build\test_cmake\CMakeFiles\test_cmake.dir\DependInfo.cmake --color=
Dependencies file "test_cmake/CMakeFiles/test_cmake.dir/test_cmake.cpp.obj.d" is newer than depends file "C:/Workspace/test_cmake_project_cmake/build/test_cmake/CMakeFiles/test_cmake.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target test_cmake
mingw32-make[2]: Leaving directory 'C:/Workspace/test_cmake_project_cmake/build'
C:/MinGW/bin/mingw32-make -f test_cmake\CMakeFiles\test_cmake.dir\build.make test_cmake/CMakeFiles/test_cmake.dir/build
mingw32-make[2]: Entering directory 'C:/Workspace/test_cmake_project_cmake/build'
[ 50%] Linking CXX executable test_cmake.exe
cd /d C:\Workspace\test_cmake_project_cmake\build\test_cmake && "C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\test_cmake.dir\link.txt --verbose=1
"C:\Program Files\CMake\bin\cmake.exe" -E rm -f CMakeFiles\test_cmake.dir/objects.a
C:\MinGW\bin\ar.exe qc CMakeFiles\test_cmake.dir/objects.a #CMakeFiles\test_cmake.dir\objects1.rsp
C:\MinGW\bin\g++.exe -g -Wl,--whole-archive CMakeFiles\test_cmake.dir/objects.a -Wl,--no-whole-archive -o test_cmake.exe -Wl,--out-implib,libtest_cmake.dll.a -Wl,--major-image-version,0,--minor-image-version,0 #CMakeFiles\test_cmake.dir\linklibs.rsp
CMakeFiles\test_cmake.dir/objects.a(test_cmake.cpp.obj): In function `main':
C:/Workspace/test_cmake_project_cmake/test_cmake/test_cmake.cpp:6: undefined reference to `boost::chrono::system_clock::now()'
C:/Workspace/test_cmake_project_cmake/test_cmake/test_cmake.cpp:11: undefined reference to `boost::chrono::system_clock::now()'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [test_cmake\CMakeFiles\test_cmake.dir\build.make:99: test_cmake/test_cmake.exe] Error 1
mingw32-make[2]: Leaving directory 'C:/Workspace/test_cmake_project_cmake/build'
mingw32-make[1]: *** [CMakeFiles\Makefile2:97: test_cmake/CMakeFiles/test_cmake.dir/all] Error 2
mingw32-make[1]: Leaving directory 'C:/Workspace/test_cmake_project_cmake/build'
mingw32-make: *** [Makefile:90: all] Error 2
Edit:
It seems it was an issue of Visual Studio Code: maybe some variables were set with wrong values and CMake could not link boost.
Finally I have created a new project and I have used a minimal CMakeLists without setting any boost variable but BOOST_ROOT (SET(BOOST_ROOT "C:\\boost") and now it works!!
Unfortunately I did not find the real reason :(, but cleaning the project and starting from scretch helped! Thanks everybody!

Why is CMake not linking pthread in this CMakeList.txt?

I get the error "undefined reference to 'pthread_create'" when running make on a program that uses pthread library. It works when I build it directly with g++:
g++ -std=c++11 -pthread pthread_Mutex.c stopwatch.o -o pthread_Mutex
But not with CMake.
I've examined a few good examples, including here: cmake and libpthread
I've tried thehouse's examples for 3.1+ and 2.8+ versions of CMake. I'm running 3.5.1 on Ubuntu. Here is my CMakeList.txt.
cmake_minimum_required(VERSION 3.5)
project(pthread_Mutex)
set (CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_executable(stopwatch ${SOURCES})
add_executable(pthread_Mutex ${SOURCES})
# I've tried this one by itself
#target_link_libraries(pthread_Mutex Threads::Threads)
if(THREADS_HAVE_PTHREAD_ARG)
target_compile_options(pthread_Mutex PUBLIC "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(pthread_Mutex "${CMAKE_THREAD_LIBS_INIT}")
endif()
target_link_libraries(pthread_Mutex pthread)
Here is my directory structure:
├── build
├── CMakeLists.txt
├── include
│   └── stopwatch.h
├── src
   ├── pthread_Mutex.cpp
    └── stopwatch.cpp
pthread_Mutex.cpp is my main program. stopwatch.cpp is a library that does not use pthreads.
But I get the following errors when running cmake VERBOSE=1:
/usr/bin/cmake -H/home/ben/workspaces/c++/thread_experiments -B/home/ben/workspaces/c++/thread_experiments/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/pthread_Mutex.dir/build.make CMakeFiles/pthread_Mutex.dir/depend
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
cd /home/ben/workspaces/c++/thread_experiments/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/pthread_Mutex.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/pthread_Mutex.dir/build.make CMakeFiles/pthread_Mutex.dir/build
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
make[2]: Nothing to be done for 'CMakeFiles/pthread_Mutex.dir/build'.
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
[ 50%] Built target pthread_Mutex
make -f CMakeFiles/stopwatch.dir/build.make CMakeFiles/stopwatch.dir/depend
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
cd /home/ben/workspaces/c++/thread_experiments/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/stopwatch.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/stopwatch.dir/build.make CMakeFiles/stopwatch.dir/build
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
[ 66%] Linking CXX executable stopwatch
/usr/bin/cmake -E cmake_link_script CMakeFiles/stopwatch.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/stopwatch.dir/src/pthread_Mutex.cpp.o CMakeFiles/stopwatch.dir/src/stopwatch.cpp.o -o stopwatch
CMakeFiles/stopwatch.dir/src/pthread_Mutex.cpp.o: In function `main':
pthread_Mutex.cpp:(.text+0x15a): undefined reference to `pthread_attr_getstacksize'
pthread_Mutex.cpp:(.text+0x19f): undefined reference to `pthread_attr_setstacksize'
pthread_Mutex.cpp:(.text+0x260): undefined reference to `pthread_create'
pthread_Mutex.cpp:(.text+0x29d): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
CMakeFiles/stopwatch.dir/build.make:120: recipe for target 'stopwatch' failed
make[2]: *** [stopwatch] Error 1
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/stopwatch.dir/all' failed
make[1]: *** [CMakeFiles/stopwatch.dir/all] Error 2
make[1]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
So I must have something wrong in the CMakeList.txt file, but I'm running out of ideas to try.
Thanks
You have used target_compile_options, that's great for the compilation flags, but you forgot to do the same for the link options. Add the same line with also target_link_options.
target_compile_options adds the flag for the compilation step. You also need to link to the pthread lib -
target_link_libraries(pthread_Mutex pthread)

Cmake cross-compiling error (missing -lm -lc and crt0.o)

I wrote a simple helloworld program in C++ (main.cpp, print.cpp, print.h) and I wanted to cross compile it to ARM using CMake. But I got an error about missing files. Below is the output of make:
/usr/bin/cmake -H/home/eslam/eslamlearning/learning/CPPtest -B/home/eslam/eslamlearning/learning/CPPtest --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/eslam/eslamlearning/learning/CPPtest/CMakeFiles /home/eslam/eslamlearning/learning/CPPtest/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
make -f CMakeFiles/LibsModule.dir/build.make CMakeFiles/LibsModule.dir/depend
make[2]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
cd /home/eslam/eslamlearning/learning/CPPtest && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest/CMakeFiles/LibsModule.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
make -f CMakeFiles/LibsModule.dir/build.make CMakeFiles/LibsModule.dir/build
make[2]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
[ 25%] Linking CXX static library libLibsModule.a
/usr/bin/cmake -P CMakeFiles/LibsModule.dir/cmake_clean_target.cmake
/usr/bin/cmake -E cmake_link_script CMakeFiles/LibsModule.dir/link.txt --verbose=1
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-ar qc libLibsModule.a CMakeFiles/LibsModule.dir/print.cpp.o
/usr/bin/ranlib libLibsModule.a
make[2]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
[ 50%] Built target LibsModule
make -f CMakeFiles/StaticTest.dir/build.make CMakeFiles/StaticTest.dir/depend
make[2]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
cd /home/eslam/eslamlearning/learning/CPPtest && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest/CMakeFiles/StaticTest.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
make -f CMakeFiles/StaticTest.dir/build.make CMakeFiles/StaticTest.dir/build
make[2]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
[ 75%] Linking CXX executable StaticTest
/usr/bin/cmake -E cmake_link_script CMakeFiles/StaticTest.dir/link.txt --verbose=1
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-g++ --sysroot=/home/eslam/ CMakeFiles/StaticTest.dir/main.cpp.o -o StaticTest libLibsModule.a
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/../lib/gcc/arm-eabi/7.3.1/../../../../arm-eabi/bin/ld: cannot find crt0.o: No such file or directory
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/../lib/gcc/arm-eabi/7.3.1/../../../../arm-eabi/bin/ld: cannot find -lm
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/../lib/gcc/arm-eabi/7.3.1/../../../../arm-eabi/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
CMakeFiles/StaticTest.dir/build.make:98: recipe for target 'StaticTest' failed
make[2]: *** [StaticTest] Error 1
make[2]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
CMakeFiles/Makefile2:107: recipe for target 'CMakeFiles/StaticTest.dir/all' failed
make[1]: *** [CMakeFiles/StaticTest.dir/all] Error 2
make[1]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
Makefile:86: recipe for target 'all' failed
make: *** [all] Error 2
Here is my CMakelists.txt
cmake_minimum_required(VERSION 3.10.2)
project (StaticTest)
enable_language(CXX)
SET(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_C_COMPILER /home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-gcc)
SET(CMAKE_CXX_COMPILER /home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-g++)
set(CMAKE_AR /home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-ar)
set(CMAKE_FIND_ROOT_PATH /home/eslam/eslamlearning/gcc-arm-eabi-toolchains)
set(CMAKE_SYSROOT /home/eslam/)
set(CMAKE_STAGING_PREFIX /home/eslam/eslamlearning/learning/CPPtest)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
ADD_LIBRARY(LibsModule STATIC
print.cpp
)
SET( APP_EXE StaticTest )
add_executable(${APP_EXE} main.cpp)
TARGET_LINK_LIBRARIES(LibsModule )
TARGET_LINK_LIBRARIES( ${APP_EXE}
LibsModule )
What am I missing? BTW, I'm not sure if my SYSROOT path is correct or not.
One last question: I was reading https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/CrossCompiling#the-toolchain-file, what is a target environment?

Make simple GLFW compiling with MinGW and cmake

I'm using cmake (clion embedded) and mingw to compile simple glfw code:
#include <GLFW/glfw3.h>
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
return 0;
}
This is my cmake file
cmake_minimum_required(VERSION 3.3)
project(hw)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_library(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
link_directories(${GLFW_LIBRARY_DIRS})
set(SOURCE_FILES main.cpp)
add_executable(hw ${SOURCE_FILES})
target_link_libraries(hw ${GLFW_LIBRARIES} )
I've included downloaded glfw for win64 files:
GLFW folder to C:\MinGW\include
lib-mingw folder content to C:\MinGW\lib
And I get following error
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" --build C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug --target all -- -j 2
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -HC:\Users\...\ClionProjects\hw -BC:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -E cmake_progress_start C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug\CMakeFiles C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 all
mingw32-make.exe[1]: Entering directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\hw.dir\build.make CMakeFiles/hw.dir/depend
mingw32-make.exe[2]: Entering directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" C:\Users\...\ClionProjects\hw C:\Users\...\ClionProjects\hw C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug\CMakeFiles\hw.dir\DependInfo.cmake --color=
mingw32-make.exe[2]: Leaving directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\hw.dir\build.make CMakeFiles/hw.dir/build
mingw32-make.exe[2]: Entering directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
[ 50%] Linking CXX executable hw.exe
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -E cmake_link_script CMakeFiles\hw.dir\link.txt --verbose=1
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -E remove -f CMakeFiles\hw.dir/objects.a
C:\MinGW\bin\ar.exe cr CMakeFiles\hw.dir/objects.a #CMakeFiles\hw.dir\objects1.rsp
C:\MinGW\bin\g++.exe -std=c++11 -g -Wl,--whole-archive CMakeFiles\hw.dir/objects.a -Wl,--no-whole-archive -o hw.exe -Wl,--out-implib,libhw.dll.a -Wl,--major-image-version,0,--minor-image-version,0 #CMakeFiles\hw.dir\linklibs.rsp
CMakeFiles\hw.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/.../ClionProjects/hw/main.cpp:5: undefined reference to `glfwInit'
C:/Users/.../ClionProjects/hw/main.cpp:6: undefined reference to `glfwWindowHint'
C:/Users/.../ClionProjects/hw/main.cpp:7: undefined reference to `glfwWindowHint'
C:/Users/.../ClionProjects/hw/main.cpp:8: undefined reference to `glfwWindowHint'
C:/Users/.../ClionProjects/hw/main.cpp:9: undefined reference to `glfwWindowHint'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [hw.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/hw.dir/all] Error 2
CMakeFiles\hw.dir\build.make:98: recipe for target 'hw.exe' failed
mingw32-make.exe[2]: Leaving directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
CMakeFiles\Makefile2:69: recipe for target 'CMakeFiles/hw.dir/all' failed
mingw32-make.exe[1]: Leaving directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
Makefile:85: recipe for target 'all' failed
mingw32-make.exe: *** [all] Error 2
This is my pretty first program c++ program with cmake and mingw, so I cant underestand what I have to do to make it just compile

CMake adding opencv

I'm trying to include opencv2.4.9 with cmake:
cmake_minimum_required(VERSION 2.8.3)
project(HydroCamel)
SET(CMAKE_CXX_FLAGS "-lpthread")
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
catkin_package()
set(SRC_FOLDER src/src)
set(ALOGS_FOLDER src/Algos)
set(ALOGS_UTILS_FOLDER src/Algos/Utils)
set(SRC_INCLUDE_FOLDER src/include)
set(DIRS ${SRC_FOLDER} ${ALOGS_FOLDER} ${ALOGS_UTILS_FOLDER} ${SRC_INCLUDE_FOLDER})
include_directories(${DIRS})
file(GLOB_RECURSE SRC_FILES "*.h" "*.cpp")
include_directories(include ${catkin_INCLUDE_DIRS})
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.55.0 COMPONENTS filesystem system thread)
#FIND_PACKAGE(OpenCV REQUIRED core imgproc highgui)
include_directories(${OpenCV_INCLUDE_DIRS})
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(SourceFiles ${SRC_FILES})
TARGET_LINK_LIBRARIES(SourceFiles ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(SourceFiles ${OpenCV_LIBS})
endif()
And I receive this error:
CMake Error at HydroCamel/CMakeLists.txt:28 (find_package): Found
package configuration file:
/home/jdorfsman/opencv-2.4.9/build/OpenCVConfig.cmake
but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered
to be NOT FOUND.
-- Configuring incomplete, errors occurred! See also "/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeOutput.log". See also
"/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeError.log".
This is the CMakeError.log file:
Determining if the pthread_create exist failed with the following output:
Change Dir: /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/make "cmTryCompileExec1061744568/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec1061744568.dir/build.make CMakeFiles/cmTryCompileExec1061744568.dir/build
make[1]: Entering directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o
/usr/bin/cc -o CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o -c /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTryCompileExec1061744568
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTryCompileExec1061744568.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o -o cmTryCompileExec1061744568 -rdynamic
CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x16): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[1]: *** [cmTryCompileExec1061744568] Error 1
make[1]: Leaving directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
make: *** [cmTryCompileExec1061744568/fast] Error 2
File /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
}
Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/make "cmTryCompileExec3426763052/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec3426763052.dir/build.make CMakeFiles/cmTryCompileExec3426763052.dir/build
make[1]: Entering directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec3426763052.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTryCompileExec3426763052.dir/CheckFunctionExists.c.o -c /usr/share/cmake-2.8/Modules/CheckFunctionExists.c
Linking C executable cmTryCompileExec3426763052
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTryCompileExec3426763052.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTryCompileExec3426763052.dir/CheckFunctionExists.c.o -o cmTryCompileExec3426763052 -rdynamic -lpthreads
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
make[1]: *** [cmTryCompileExec3426763052] Error 1
make[1]: Leaving directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
make: *** [cmTryCompileExec3426763052/fast] Error 2
Looks like you don't specify the pthread flag.
CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x16): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[1]: *** [cmTryCompileExec1061744568] Error 1
make[1]: Leaving directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
make: *** [cmTryCompileExec1061744568/fast] Error 2
try add:
SET(CMAKE_CXX_FLAGS "-lpthread")