I'm trying to include the GMP library into my project.
This is my folder tree:
As you can see, the gmp folder (that contains include and lib) is into the include folder.
This is my app/CMakeLists.txt:
cmake_minimum_required(VERSION 3.18)
add_executable(
${PROJECT_NAME}
../include/KMC/kmc_api/kmc_file.cpp
../include/KMC/kmc_api/kmer_api.cpp
../include/KMC/kmc_api/mmer.cpp
main.cpp
)
include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src
)
target_link_directories(${PROJECT_NAME} PUBLIC
${CMAKE_SOURCE_DIR}/include/gmp/lib
)
target_link_libraries(${PROJECT_NAME} PUBLIC
gmp
gmpxx
)
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_SOURCE_DIR}/include/gmp/include
)
target_compile_definitions(${PROJECT_NAME} PRIVATE DEBUG=1)
and this, is the CMakeLists.txt in the project root:
cmake_minimum_required(VERSION 3.18)
set(CMAKE_CXX_COMPILER "/usr/local/bin/g++")
project(MyProj)
enable_language(CXX)
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 1)
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wpedantic -pedantic-errors -fopenmp")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
include(CMakeToolsHelpers OPTIONAL)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories(include)
include_directories(include/KMC)
add_subdirectory(app)
After, cd build && cmake .. I run make -j12 and than, -the compilation fail:
[100%] Linking CXX executable DynaDBG
cd /Users/th3g3ntl3man/Desktop/Repository/DynaDBG/build/app && /usr/local/Cellar/cmake/3.18.4/bin/cmake -E cmake_link_script CMakeFiles/DynaDBG.dir/link.txt --verbose=1
/usr/local/bin/g++ -Wall -Wextra -Wpedantic -pedantic-errors -fopenmp -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/DynaDBG.dir/__/include/KMC/kmc_api/kmc_file.cpp.o CMakeFiles/DynaDBG.dir/__/include/KMC/kmc_api/kmer_api.cpp.o CMakeFiles/DynaDBG.dir/__/include/KMC/kmc_api/mmer.cpp.o CMakeFiles/DynaDBG.dir/main.cpp.o -o DynaDBG -L/Users/th3g3ntl3man/Desktop/Repository/DynaDBG/include/gmp/lib -Wl,-rpath,/Users/th3g3ntl3man/Desktop/Repository/DynaDBG/include/gmp/lib -lgmp -lgmpxx
Undefined symbols for architecture x86_64:
"__ZlsRSoPK12__mpz_struct", referenced from:
__ZlsIA1_12__mpz_structS1_ERSoS2_RK10__gmp_exprIT_T0_E in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [app/CMakeFiles/DynaDBG.dir/build.make:151: app/DynaDBG] Error 1
make[2]: Leaving directory '/Users/th3g3ntl3man/Desktop/Repository/DynaDBG/build'
make[1]: *** [CMakeFiles/Makefile2:116: app/CMakeFiles/DynaDBG.dir/all] Error 2
make[1]: Leaving directory '/Users/th3g3ntl3man/Desktop/Repository/DynaDBG/build'
make: *** [Makefile:106: all] Error 2
UPDATE:
Compiling the example I have this error:
Related
I am trying to run cmake command in the new environment.
Upon running cmake . . I get clang: error: the clang compiler does not support '-march=core2' error. I tried to run it with cmake '-DCMAKE_CXX_FLAGS='-march=x86-64'' flag, and it changed the march for a while but only in CLion IDE and only when I was doing that continuously. I can't seem to make it work in the terminal while reading different makefile.
Full error when I run either cmake or ```cmake '-DCMAKE_CXX_FLAGS='-march=x86-64'':
Determining if the C compiler works failed with the following output:
Change Dir: /Users/marcin/CLionProjects/pythontest5/CMakeFiles/CMakeTmp
Run Build Command(s):/Library/Developer/CommandLineTools/usr/bin/make -f Makefile cmTC_925c1/fast && /Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/cmTC_925c1.dir/build.make CMakeFiles/cmTC_925c1.dir/build
Building C object CMakeFiles/cmTC_925c1.dir/testCCompiler.c.o
/Library/Developer/CommandLineTools/usr/bin/gcc -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -isystem /opt/homebrew/anaconda3/include -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -MD -MT CMakeFiles/cmTC_925c1.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_925c1.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_925c1.dir/testCCompiler.c.o -c /Users/marcin/CLionProjects/pythontest5/CMakeFiles/CMakeTmp/testCCompiler.c
clang: error: the clang compiler does not support '-march=core2'
make[1]: *** [CMakeFiles/cmTC_925c1.dir/testCCompiler.c.o] Error 1
make: *** [cmTC_925c1/fast] Error 2
My CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
project(automobile VERSION 0.1.0)
# Include dir
include_directories(/usr/local/include)
# Src
AUX_SOURCE_DIRECTORY(src SRC_FILES)
# Headers
set(PROJECT_SOURCE_DIR "src")
set(PROJECT_INCLUDE_DIR "include/automobile_bits")
# Source files
set(SOURCE_FILES
${PROJECT_INCLUDE_DIR}/motorcycle.hpp
${PROJECT_SOURCE_DIR}/motorcycle.cpp
)
# Set up such that XCode organizes the files correctly
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_FILES})
# Add library
add_library(automobile SHARED ${SOURCE_FILES})
# Include directories
target_include_directories(automobile PRIVATE include/automobile_bits)
# Install
install(TARGETS automobile DESTINATION lib)
# Install the headers
install(FILES include/automobile DESTINATION include)
# Create base directory
install(DIRECTORY include/automobile_bits DESTINATION include)
It's unclear where cmake gets -march=core2 from. Try to remove CMakeCache.txt then re-run cmake ..
If it does not help, try to remove CMakeCache.txt and run cmake . -DCMAKE_OSX_ARCHITECTURES=arm64.
I tried to test OpenMP on MacOS with the Apple Clang compiler that comes with Xcode.
Code:
#include <iostream>
#include <cmath>
#include </usr/local/opt/libomp/include/omp.h>
int main() {
std::cout << "Hello, World!" << std::endl;
#pragma omp parallel
{
printf("Hello World! \n");
}
return 0;
}
and this is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.19)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
include (FindOpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
message (VERBOSE "OpenML library found")
else()
message (FATAL_ERROR "OpenML library not found (required for multithreading)")
endif ()
add_executable(untitled main.cpp)
This is the compiler's error:
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
Undefined symbols for architecture x86_64:
"___kmpc_fork_call", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2
The OpenMP is installed using hombrew libomp I also tried adding environment variable LDFLAGS=-L/usr/local/opt/libomp/lib/ and it does not affect the result
UPDATE: COMPILATION WITH VERBOSE=1:
...
[100%] Linking CXX executable untitled
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_link_script CMakeFiles/untitled.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++ -Xclang -fopenmp -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names -L/usr/local/opt/libomp/lib/ CMakeFiles/untitled.dir/main.cpp.o -o untitled
Undefined symbols for architecture x86_64:
"___kmpc_fork_call", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2
By using set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") and using set in other places, you are replacing the flags that CMAKE puts. It is better to append in this case rather than replace. You can use add_compile_options(-Wall) here.
As mentioned by #Tsyvarev, it is perhaps better to use imported library. Here are a few other ways you can use target_link_libraries.
For your particular case, it is best to replace
find_package(OpenMP REQUIRED)
where you currently have include (FindOpenMP). Further, remove the if condition as well. Finally, add the following to the end of your CMakeLists.txt file.
target_link_libraries(
untitled
PRIVATE
OpenMP::OpenMP_CXX
)
Here is a good explanation of the difference between PRIVATE and PUBLIC.
Your final CMakeLists.txt file should look like:
cmake_minimum_required(VERSION 3.19)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenMP REQUIRED)
add_executable(
untitled
main.cpp
)
target_link_libraries(
untitled
PRIVATE
OpenMP::OpenMP_CXX
)
Thanks to Tsyvarev, for helping me with CMake a few months ago. Further, thanks to the answers that Tsyvarev points toward, which my answer is based on (along with my own testing).
I would like to compile my own project. I have error with **make**. cmake .. works properly.
I tried to compile the example in /Onboard-SDK/sample/platform/telemetry.
I did mkdir build and cd build in telemetry directory.
This is my CMakeList.txt
cmake_minimum_required(VERSION 2.8)
project(djiosdk-telemetry-sample)
# Compiler flags: link with pthread and enable C++11 support
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -g -O0")
# Tell the project where osdk-core is located
if(NOT ONBOARDSDK_SOURCE)
set(ONBOARDSDK_SOURCE "/home/pi/Onboard-SDK/osdk-core")
#set(ONBOARDSDK_SOURCE "/usr/local/include")
endif()
# Specify locations for osdk-core headers
include_directories(${ONBOARDSDK_SOURCE}/api/inc)
include_directories(${ONBOARDSDK_SOURCE}/utility/inc)
include_directories(${ONBOARDSDK_SOURCE}/hal/inc)
include_directories(${ONBOARDSDK_SOURCE}/protocol/inc)
include_directories(${ONBOARDSDK_SOURCE}/platform/linux/inc)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../hal)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../osal)
include_directories(include /usr/local/include)
include_directories(${ONBOARDSDK_SOURCE}/../sample/core/inc)
# User-code related project files
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common)
FILE(GLOB SOURCE_FILES *.hpp *.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../common/dji_linux_environment.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../common/dji_linux_helpers.cpp
main.cpp
telemetry_sample.cpp
)
if (OSDK_HOTPLUG)
FILE(GLOB SOURCE_FILES ${SOURCE_FILES} ${CMAKE_CURRENT_SOURCE_DIR}/../hal/hotplug/*.c)
endif ()
# Target and linking
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} djiosdk-core)
This is the result after make
[ 20%] Linking CXX executable djiosdk-telemetry-sample
/usr/bin/ld: //usr/local/lib/libdjiosdk-core.a(dji_legacy_linker.cpp.o): in function `DJI::OSDK::LegacyLinker::legacyX5SEnableTask(void*)':
/home/pi/Onboard-SDK/osdk-core/api/src/dji_legacy_linker.cpp:69: **undefined reference to `DJI::OSDK**::Linker::getLocalSenderId()'
/usr/bin/ld: /home/pi/Onboard-SDK/osdk-core/api/src/dji_legacy_linker.cpp:71: **undefined reference** to `OsdkOsal_TaskSleepMs'
/usr/bin/ld: /home/pi/Onboard-SDK/osdk-core/api/src/dji_legacy_linker.cpp:72: **undefined reference** to `**DJI::OSDK**::Linker::isUSBPlugged()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/djiosdk-telemetry-sample.dir/build.make:129: djiosdk-telemetry-sample] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/djiosdk-telemetry-sample.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I often have [100%] Linking CXX executable djiosdk-telemetry-sample.
The main problem is with "undefined reference".
I create CMakeList.txt according to: https://developer.dji.com/onboard-sdk/documentation/development-workflow/integrate-sdk.html
What I should do?
The proper CMakeList.txt for creating your own project (example based on telemetry sample):
cmake_minimum_required(VERSION 2.8)
project(djiosdk-telemetry-sample)
set(ARCH armv7)
# Compiler flags: link with pthread and enable C++11 support
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -g -O0")
# Tell the project where osdk-core is located
if(NOT ONBOARDSDK_SOURCE)
set(ONBOARDSDK_SOURCE "/home/pi/Onboard-SDK/osdk-core")
endif()
set(CURRENT_CMAKE_MODULE_PATH ${ONBOARDSDK_SOURCE}/cmake-modules)
# Specify locations for osdk-core headers
include_directories(${ONBOARDSDK_SOURCE}/api/inc)
include_directories(${ONBOARDSDK_SOURCE}/utility/inc)
include_directories(${ONBOARDSDK_SOURCE}/hal/inc)
include_directories(${ONBOARDSDK_SOURCE}/protocol/inc)
include_directories(${ONBOARDSDK_SOURCE}/platform/linux/inc)
include_directories(${ONBOARDSDK_SOURCE}/linker/${ARCH}/inc)
include_directories(${ONBOARDSDK_SOURCE}/../sample/platform/linux/hal)
include_directories(${ONBOARDSDK_SOURCE}/../sample/platform/linux/hal/hotplug)
include_directories(${ONBOARDSDK_SOURCE}/../sample/platform/linux/osal)
include_directories(include /usr/local/include)
include_directories(include /usr/local/lib)
include_directories(${ONBOARDSDK_SOURCE}/../sample/core/inc)
# User-code related project files
include_directories(${ONBOARDSDK_SOURCE}/../sample/platform/linux/common)
FILE(GLOB SOURCE_FILES *.hpp *.cpp
${ONBOARDSDK_SOURCE}/../sample/platform/linux/common/dji_linux_environment.cpp
${ONBOARDSDK_SOURCE}/../sample/platform/linux/common/dji_linux_helpers.cpp
${ONBOARDSDK_SOURCE}/../sample/platform/linux/hal/osdkhal_linux.c
${ONBOARDSDK_SOURCE}/../sample/platform/linux/osal/osdkosal_linux.c
)
if (OSDK_HOTPLUG)
FILE(GLOB SOURCE_FILES ${SOURCE_FILES} ${ONBOARDSDK_SOURCE}/../sample/platform/linux/hal/hotplug/*.c)
endif ()
link_libraries(dji-linker)
link_directories(/usr/local/include /usr/local/lib)
link_libraries(${ONBOARDSDK_SOURCE}/linker/${ARCH}/lib/libdji-linker.a)
set(CMAKE_MODULE_PATH ${CURRENT_CMAKE_MODULE_PATH})
find_package(LibUSB REQUIRED)
find_package(FFMPEG REQUIRED)
include_directories(${ADVANCED_SENSING_HEADERS_DIR})
# Target and linking
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} PUBLIC djiosdk-core)
target_link_libraries(${PROJECT_NAME} PUBLIC dji-linker)
target_link_libraries(${PROJECT_NAME} PUBLIC ${ONBOARDSDK_SOURCE}/linker/${ARCH}/lib/libdji-linker.a)
target_include_directories(${PROJECT_NAME} PUBLIC ${LIBUSB_1_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${LIBUSB_1_LIBRARIES})
target_link_libraries(${PROJECT_NAME} PUBLIC ${FFMPEG_LIBRARIES})
target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${ADVANCED_SENSING_HEADERS_DIR}>)
target_link_libraries(${PROJECT_NAME} PRIVATE advanced-sensing)
target_include_directories(${PROJECT_NAME} PUBLIC ${FFMPEG_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC ${FFMPEG_LIBRARIES})
I get the following error when compiling my program c++ with tesseract and opencv:
make
[ 50%] Linking CXX executable car_license_plates
ld: library not found for -llept
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [car_license_plates] Error 1
make[1]: *** [CMakeFiles/car_license_plates.dir/all] Error 2
make: *** [all] Error 2
My system is: MacOS X Darwin - 15.4.0 - x86_64
The CMakeLists.txt is the follow:
cmake_minimum_required(VERSION 2.8)
set(CMAKE_BUILD_TYPE Release)
project( car_license_plates )
set(CMAKE_CXX_FLAGS "-Wall -O2 -std=c++0x ")
add_executable( ${PROJECT_NAME} car_license_plates.cpp )
# find opencv package
find_package( OpenCV REQUIRED )
# includes
set(LEPTONICA_INCLUDE_PATH "/usr/local/Cellar/leptonica/1.73/include/leptonica")
set(TESSERACT_INCLUDE_PATH "/usr/local/Cellar/tesseract/3.04.01_2/include/tesseract")
include_directories(${OpenCV_INCLUDE_DIRS} ${LEPTONICA_INCLUDE_PATH} ${TESSERACT_INCLUDE_PATH})
# libraries
set(LEPTONICA_LIBS "lept")
set(TESSERAC_LIBS "tesseract")
set(LIBS ${LIBS} ${OpenCV_LIBS} ${LEPTONICA_LIBS} ${TESSERAC_LIBS})
target_link_libraries(${PROJECT_NAME} ${LIBS})
where are the mistake?
I am trying to build a cmakelist file from makefile of leap motion, I can compile in a specific directory, I need to copy /include/ and /lib/x64/ directories . The makefile is the follow:
LEAP_LIBRARY := ./lib/x64/libLeap.so -Wl,-rpath,./lib/x64
Sample: Sample.cpp
$(CXX) -Wall -g -I include Sample.cpp -o Sample $(LEAP_LIBRARY)
I have tried to build a cmakelist file as follows:
cmake_minimum_required(VERSION 2.8)
project(Sample)
INCLUDE_DIRECTORIES(/include/)
LINK_DIRECTORIES(/lib/x64/)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -I /include/ -Wl,-rpath,./lib/x64")
add_executable(Sample Sample.cpp )
target_link_libraries(Sample libLeap.so)
But I always get the same error:
Linking CXX executable Sample
/usr/bin/ld: can't find -lLeap
collect2: error: ld returned 1 exit status
make[2]: *** [Sample] Error 1
make[1]: *** [CMakeFiles/Sample.dir/all] Error 2
make: *** [all] Error 2
Thanks and regards.
I could solve with following lines:
cmake_minimum_required(VERSION 2.8)
project(Sample)
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
INCLUDE_DIRECTORIES(../include/)
LINK_DIRECTORIES(../lib/x64/)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
add_executable(Sample Sample.cpp )
target_link_libraries(Sample -lLeap)
This solution has warnings but works:
CMake Warning (dev) at CMakeLists.txt:5 (LINK_DIRECTORIES):
This command specifies the relative path
../lib/x64
as a link directory.
Policy CMP0015 is not set: link_directories() treats paths relative to the
source dir. Run "cmake --help-policy CMP0015" for policy details. Use the
cmake_policy command to set the policy and suppress this warning.
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
-- Generating done
-- Build files....
Two way to delete this warning:
cmake_minimum_required(VERSION 2.8)
project(Sample)
cmake_policy(SET CMP0015 NEW)
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
INCLUDE_DIRECTORIES(../include/)
LINK_DIRECTORIES(lib/x64/)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
add_executable(Sample Sample.cpp )
target_link_libraries(Sample -lLeap)
Or use cmake .. -Wno-dev