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
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 got the following error with the first running of the command
cmake .
But if I run the command again, the compilation succeed. That is, I have to run cmake twice for make the project compiled. Why is it so? How could I fix it?
Error message:
CMakeFiles/exec.dir/Timer.cpp.o: In function `std::thread::thread<Timer::Timer()::{lambda()#1}>(Timer::Timer()::{lambda()#1}&&)':
/usr/include/c++/7/thread:122: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/exe.dir/build.make:146: recipe for target 'exe' failed
make[2]: *** [exe] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/exe.dir/all' failed
make[1]: *** [CMakeFiles/exe.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -g -std=c++14 -std=c++11 -std=c++17 ")
project(myproject)
set(SOURCE_FILES main.cpp Timer.cpp TimerEntity.cpp)
add_executable(exe ${SOURCE_FILES})
It is not usually noted, but appending compiler flags via
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} <...>")
should be placed after the project() call.
Exactly project() call sets the initial value of CMAKE_CXX_FLAGS variable.
Normally, project() call is issued just after the cmake_minimum_required():
cmake_minimum_required(VERSION 3.10)
project(437_Hw1)
...
Details
In your current code, when you call cmake the first time, your first setting set(CMAKE_CXX_FLAGS ...) is replaced by the value of the following project() call and stored in the CACHE.
When you call cmake the second time, CMAKE_CXX_FLAGS variable is already set (it is loaded from the cache). So, set(CMAKE_CXX_FLAGS ...) sets the variable to correct value. Moreover, following project() doesn't change CMAKE_CXX_FLAGS variable because it finds it already in the cache.
I reworked your CMakeLists.txt to use modern CMake:
cmake_minimum_required(VERSION 3.7)
project(437_Hw1)
# CMake can set the standard in a cross-platform way for you
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Debug)
set(SOURCE_FILES main.cpp SubTimer.cpp TimerEntity.cpp)
add_executable(exe ${SOURCE_FILES})
# These two lines add the platform-specific magic for the right threading library/headers.
find_package(Threads)
target_link_libraries(exe Threads::Threads)
I suggest you replace the line
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -g -std=c++14 -std=c++11 -std=c++17 ")
with
set(CMAKE_CXX_STANDARD 17)
target_link_libraries(exe pthread)
and see if that helps.
I'm trying to use CMake with a CUDA project of mine, but I'm having trouble getting it to build the executable when compiled on a system that has a CUDA-enabled device.
The CMakeLists.txt in question is below. It supports systems with and without CUDA-enabled devices, and builds just fine on my Macbook which doesn't have CUDA.
cmake_minimum_required (VERSION 2.8)
message(STATUS "CMake version: ${CMAKE_VERSION}")
project(stockModel)
# Grab the CUDA package
find_package(CUDA)
set(GPU_ACCELERATED ${CUDA_FOUND})
# Set directory and compilation flags for both g++ and nvcc
set(CMAKE_BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}
-gencode arch=compute_50,code=sm_50; -std=c++11; -lcurand;"
)
set(CUDA_PROPAGATE_HOST_FLAGS off)
# Add directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/build/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/core/)
if (${GPU_ACCELERATED})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/support/)
endif()
# Setup environments, depending on GPU accel. status
set(SRCS build/main.cpp core/callModels.cpp)
set(INCS core/callModels.h)
if (${GPU_ACCELERATED})
set(SRCS ${SRCS} support/prng.cu support/debugCFP.cu)
set(INCS ${INCS} support/prng.h support/debugCFP.h)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/core/callModels.cpp
PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ
)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}
-L/usr/local/cuda/lib64 -lcuda -lcudart"
)
endif()
# Create executable
message(STATUS "Sources: ${SRCS}")
message(STATUS "Includes: ${INCS}")
cuda_add_executable(stockModel ${SRCS} ${INCS})
The error I get when I attempt to build on my Jetson TX1 is as follows:
...
[ 80%] Building CXX object CMakeFiles/stockModel.dir/main.cpp.o
[100%] Linking CXX executable stockModel
c++: fatal error: no input files
compilation terminated.
...
Any ideas as to what is going wrong here? Obviously it has something to do with the CUDA 'extras', but I'm at a loss as to what is causing this.
Let me know if you need more details.
Here is the relevant part of the verbose output:
...
[100%] Linking CXX executable stockModel
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/stockModel.dir/link.txt --verbose=1
/usr/bin/c++ -std=c++11 -pthread
c++: fatal error: no input files
compilation terminated.
I've uploaded the full make VERBOSE=1 output to this gist on GitHub.
CMake is sometimes finicky about spaces and list combinations. I know that doesn't sound like much of an explanation, but I'm not much of an expert.
What you need to do is replace this:
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}
-L/usr/local/cuda/lib64 -lcuda -lcudart"
with this:
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/cuda/lib64 -lcuda -lcudart")
(single line). That should do it. At least - it does on my system (I created dummy source files with your files' names to try this out).
I'm trying to compile a program that uses the URG (Laser scanner) library along with PCL. URG uses make to build but PCL uses cmake. I've been trying to use cmake for both but i've been having issues.
I found FindURG.cmake and put it in the modules folder here: https://github.com/wicron/vlidar/blob/master/cmake/FindURG.cmake
My CMakeLists is:
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(MY_GRAND_PROJECT)
find_package(PCL 1.3 REQUIRED COMPONENTS common io)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
find_package(URG REQUIRED)
include_directories(${URG_INCLUDE_DIR})
link_directories(${URG_LIBRARY})
#add_executable(pcd_write_test pcd_write.cpp)
add_executable(urg_read_test gdScanSample.cpp)
#target_link_libraries(pcd_write_test ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES})
target_link_libraries(urg_read_test ${URG_LIBRARY})
SET(CMAKE_C_FLAGS "-I/usr/local/include/urg")
#SET(CMAKE_CXX_FLAGS "-I/usr/local/include/urg")
PCL is found fine, URG is also found as shown below. The directories look fine too.
root#CCSL02:/home/marwan/pcl_sample# cmake CMakeLists.txt
-- looking for PCL_COMMON
-- looking for PCL_OCTREE
-- looking for PCL_IO
-- Found c_urg libraries. /usr/local/lib/libc_urg_system.so/usr/local/lib/libc_urg.so/usr/local/lib/libc_urg_connection.so/usr/lib/liburg.so/usr/lib/liburg_connection.so/usr/lib/liburg_system.so/usr/lib/liburg_common.so/usr/lib/liburg_coordinate.so/usr/lib/liburg_geometry.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/marwan/pcl_sample
But as soon as I run make, here's what I get:
root#CCSL02:/home/marwan/pcl_sample# make
[100%] Building CXX object CMakeFiles/urg_read_test.dir/gdScanSample.cpp.o
/home/marwan/pcl_sample/gdScanSample.cpp:10:21: fatal error: UrgCtrl.h: No such file or directory compilation terminated.
make[2]: *** [CMakeFiles/urg_read_test.dir/gdScanSample.cpp.o] Error 1
make[1]: *** [CMakeFiles/urg_read_test.dir/all] Error 2
make: *** [all] Error 2
I've tried to play with the CMakeLists in which I tried added the CXX flags but no luck
It should be noted that the following makefile compiles the urg program normally
# Makefile for urg_sample
# Satofumi KAMIMURA
# $Id: Makefile 1997 2012-10-30 02:57:51Z satofumi $
CXXFLAGS = -g -O0 -Wall -Werror `urg-config --cflags` `sdl-config --cflags`
LDFLAGS =
LDLIBS = `urg-config --libs` `sdl-config --libs` -lc
TARGET = gdScanSample
all : $(TARGET)
clean :
$(RM) *.o $(TARGET)
.PHONY : all clean
You need to verify that SET(CMAKE_C_FLAGS "-I/usr/local/include/urg") points to the directory where all the h files are. Maybe you will need to add more than one location.
Solved thanks to ComicSansMS
Here's a working CMakeLists.txt:
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(MY_GRAND_PROJECT)
find_package(PCL 1.3 REQUIRED)#COMPONENTS common io)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
find_package(URG REQUIRED)
include_directories(${URG_INCLUDE_DIR}/urg)
link_directories(${URG_LIBRARIES})
add_executable(pcd_write_test pcd_write.cpp)
target_link_libraries(pcd_write_test ${PCL_LIBRARIES} ${URG_LIBRARIES})
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
I have a static library called sdpAPI.a
I am trying to link my cpp file to it using cmake.
My CMakeLists.txt looks like this?
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(demo_project CXX)
IF(CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_C_FLAGS "-Wall -Wextra -Wunreachable-code -O0 -D_DEBUG -ggdb -m32")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
INCLUDE_DIRECTORIES(sdpapi)
LINK_DIRECTORIES(~/projects/test_sdp/sdpapi)
SET(source_files main.cpp)
SET(libs sdpAPI)
ADD_EXECUTABLE(demo ${source_files})
TARGET_LINK_LIBRARIES(demo ${libs})
And my sdpAPI.a is located in this directory test_sdp/sdpapi/sdpAPI.a
The error I am getting is the following:
[100%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
Linking CXX executable demo
/usr/bin/ld: cannot find -lsdpAPI
collect2: ld returned 1 exit status
make[2]: *** [demo] Error 1
make[1]: *** [CMakeFiles/demo.dir/all] Error 2
make: *** [all] Error 2
Can anyone see anything obvious that I am doing wrong.
I should have renamed sdpAPI.a to libsdpAPI.a
This solved my problem. A silly mistake which cost me 3 hours.
Hope this helps someone else.
Additional advice for previous answer.
To understand what's going on with compilation/linking just run make with VERBOSE=1 option to see full command used by make.
And of course link options for gcc
Use $ENV{HOME} instead of ~.