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

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?

Related

Cross compiling for arm android

i want to build c++ library for my android device, more specific i want to build taglib to use with my Qt project, i have a music app that uses taglib, i build it for windows and ubuntu and it works as expected, but now i want to cross compile taglib to use on my android phone, i searched hours to find the solution, i found some article about toolchain file and manual cmake option but none of them worked for me,
i tried to set CMAKE_SYSTEM_NAME to android and set other Cmake option like below:
set(CMAKE_SYSTEM_NAME Android)
message("this is host system ${CMAKE_SYSTEM_NAME} ${CMAKE_HOST_SYSTEM_NAME} {CMAKE_SYSTEM_VERSION}")
set(CMAKE_SYSTEM_VERSION 21)
set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a)
set(CMAKE_ANDROID_NDK /home/sub/Downloads/android/android-ndk-r20)
set(CMAKE_ANDROID_STL_TYPE gnustl_static)
and when i check the generated .so file with file command it show my system info like below:
libmylib.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=4245460a39baea2cbd1f28c3a2fd8037b07fe995, with debug_info, not stripped
so can anyone give me some link or help to do this?
** update
my cmake command is like below:
cmake CMakeLists.txt -DCMAKE_SYSTEM_NAME=ANDROID -DCMAKE_TOOLCHAIN_FILE=/home/sub/Downloads/android-ndk-r21/build/cmake/android.toolchain.cmake
now i use new ndk and use it android.toolchain.cmake file but the output say it doesn't use it:
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_TOOLCHAIN_FILE
-- Build files have been written to: /home/sub/Documents/Projects/cpp/build-for-android
my new CMakeLists.txt is :
cmake_minimum_required(VERSION 3.17)
project(addlib CXX)
# set(CMAKE_SYSTEM_NAME Android)
# message("this is host system ${CMAKE_SYSTEM_NAME} ${CMAKE_HOST_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION}")
# set(CMAKE_SYSTEM_VERSION 21)
# set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a)
# set(CMAKE_ANDROID_NDK /home/sub/Downloads/android/android-ndk-r20)
# set(CMAKE_ANDROID_STL_TYPE gnustl_static)
set(BUILD_SHARED_LIBS true)
add_library(mylib main.cpp)
target_include_directories(mylib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
message(${CMAKE_CROSSCOMPILING})
install(TARGETS mylib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/)
make command output with verbose on is same as below:
/usr/local/bin/cmake -S/home/sub/Documents/Projects/cpp/build-for-android -B/home/sub/Documents/Projects/cpp/build-for-android --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/sub/Documents/Projects/cpp/build-for-android'
make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/depend
make[2]: Entering directory '/home/sub/Documents/Projects/cpp/build-for-android'
cd /home/sub/Documents/Projects/cpp/build-for-android && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles/mylib.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/sub/Documents/Projects/cpp/build-for-android'
make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/build
make[2]: Entering directory '/home/sub/Documents/Projects/cpp/build-for-android'
[ 50%] Linking CXX shared library libmylib.so
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/mylib.dir/link.txt --verbose=1
/usr/bin/g++ -fPIC -g -shared -Wl,-soname,libmylib.so -o libmylib.so CMakeFiles/mylib.dir/main.cpp.o
make[2]: Leaving directory '/home/sub/Documents/Projects/cpp/build-for-android'
[100%] Built target mylib
make[1]: Leaving directory '/home/sub/Documents/Projects/cpp/build-for-android'
/usr/local/bin/cmake -E cmake_progress_start /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles 0
thank you

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)

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

error linking GMP library

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)

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")