I have a problem with compiling my program with boost and cmake. I use cross-compilation with using gcc-linaro-arm-linux-gnueabihf-4.9/bin/arm-linux-gnueabihf-g++ compiler.
And now I have cmake file like:
cmake_minimum_required (VERSION 2.6.2)
project (xxx)
SET(CMAKE_CXX_COMPILER /home/kamil/gcc-linaro-arm-linux-gnueabihf-4.9/bin/arm-linux-gnueabihf-g++)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -Dtypeof=__typeof__ -D_GLIBCXX_USE_CXX11_ABI=0 -Wall -Werror -Wextra -Wno-error=array-bounds")
if(DEFINED ENV{SDKTARGETSYSROOT})
set(CMAKE_FIND_ROOT_PATH $ENV{SDKTARGETSYSROOT})
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
endif(DEFINED ENV{SDKTARGETSYSROOT})
find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories("/usr/local/include")
include_directories(${Boost_INCLUDE_DIRS})
enable_testing()
set(CMAKE_CTEST_COMMAND ctest -V)
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} )
add_subdirectory(src)
add_subdirectory(test EXCLUDE_FROM_ALL)
and
add_executable(xxxx
main.cpp
...cpp
...cpp
...cpp
...cpp
)
target_link_libraries(xxxx
${Boost_SYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
)
install(TARGETS xxxx
DESTINATION bin
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
)
And now when use commands in linux(Ubuntu):
cmake ..
make
I have response at:
kamil#kamil:~/test/build$ rm -rf *
kamil#kamil:~/test/build$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Boost version: 1.55.0
-- Found the following Boost libraries:
-- system
-- filesystem
-- Found GTest: /usr/local/lib/libgtest.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/kamil/test/build
kamil#kamil:~/test/build$ make
Scanning dependencies of target xxxx
[ 12%] Building CXX object src/CMakeFiles/test.dir/main.cpp.o
In file included from /home/test/src/test/utils/logger.hpp:4:0,
from /home/kamil/test/src/main.cpp:9:
/home/kamil/test/src/test/utils/singleton.hpp:5:33: fatal error: boost/noncopyable.hpp: No such file or directory
#include <boost/noncopyable.hpp>
^
compilation terminated.
make[2]: *** [src/CMakeFiles/test.dir/main.cpp.o] Error 1
make[1]: *** [src/CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
Error:
fatal error: boost/noncopyable.hpp: No such file or directory
#include <boost/noncopyable.hpp>
When I comment on the following line in cmake:
SET(CMAKE_CXX_COMPILER /home/kamil/toradex/gcc-linaro-arm-linux-gnueabihf-4.9/bin/arm-linux-gnueabihf-g++)
then everything is ok.
I install boost in Ubuntu with command: sudo apt-get install libboost1.55-all-dev
What is wrong with compiling with linux-gnueabihf-g++ and how to fix it.
Compiling boost with gcc-linaro-arm-linux-gnueabihf-4.9 worked.
Helps link: http://www.cnx-software.com/2011/10/03/cross-compiling-boost-c-libraries-for-arm/
Related
Why does Cmake find the library but I can't include it?
I am trying to use the gsl library in C++ but I get gsl/gsl_sf_bessel.h: No such file or directory when I run cmake --build even though it says -- Found GSL: /usr/include (found version "2.7.1") in the terminal after running cmake ...
I am using Ubuntu 22.04
I installed gsl with sudo apt-get install libgsl-dev and it is in /usr/include.
I think it might be a problem with the compiler I am using but I'm not sure how to check.
My CMakeLists.txt file
cmake_minimum_required(VERSION 3.5.1)
project(mujoco_gym)
set(CMAKE_CXX_STANDARD 14)
# It prevents the decay to C++98 when the compiler does not support C++14
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# It disables the use of compiler-specific extensions
# e.g. -std=c++14 rather than -std=gnu++14
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
find_package(GSL REQUIRED)
link_libraries(GSL::gsl)
include_directories(${PROJECT_SOURCE_DIR})
file(GLOB SOURCE_FILES mujoco_gym.cpp)
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries (
${CMAKE_PROJECT_NAME}
${GSL_LIBRARIES}
)
My .cpp file.
#include<stdbool.h> //for bool
#include "stdlib.h"
#include "string.h"
#include <iostream>
#include <gsl/gsl_sf_bessel.h>
int main()
{
double x = 5.0;
std::cout << gsl_sf_bessel_j0(x);
return 0;
}
Cmake and build in the terminal
$ cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/iii/miniconda3/envs/tf/bin/x86_64-conda-linux-gnu-cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/iii/miniconda3/envs/tf/bin/x86_64-conda-linux-gnu-c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2")
-- Found GSL: /usr/include (found version "2.7.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/iii/.mujoco/mujoco210/sample/build
$ cmake --build . --config Release
[ 50%] Building CXX object CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o
/home/iii/.mujoco/mujoco210/sample/mujoco_gym.cpp:21:10: fatal error: gsl/gsl_sf_bessel.h: No such file or directory
21 | #include <gsl/gsl_sf_bessel.h>
| ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/mujoco_gym.dir/build.make:76: CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/mujoco_gym.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
$
Trying to build samples/mqtt/basic_pub_sub sample from aws-iot-device-sdk-cpp-v2 which contains CMakeList.txt:
cmake_minimum_required(VERSION 3.1)
# note: cxx-17 requires cmake 3.8, cxx-20 requires cmake 3.12
project(basic-pub-sub CXX)
file(GLOB SRC_FILES
"*.cpp"
)
add_executable(${PROJECT_NAME} ${SRC_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 14)
#set warnings
if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX /wd4068)
else ()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wno-long-long -pedantic -Werror)
endif ()
find_package(aws-crt-cpp REQUIRED)
target_link_libraries(${PROJECT_NAME} AWS::aws-crt-cpp)
Trying to build:
mkdir build
cd build
cmake ..
Got errro:
-- The CXX compiler identification is GNU 9.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:21 (find_package):
By not providing "Findaws-crt-cpp.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"aws-crt-cpp", but CMake did not find one.
Could not find a package configuration file provided by "aws-crt-cpp" with
any of the following names:
aws-crt-cppConfig.cmake
aws-crt-cpp-config.cmake
Add the installation prefix of "aws-crt-cpp" to CMAKE_PREFIX_PATH or set
"aws-crt-cpp_DIR" to a directory containing one of the above files. If
"aws-crt-cpp" provides a separate development package or SDK, be sure it
has been installed.
-- Configuring incomplete, errors occurred!
I found aws-crt-cpp-config.cmake if folder home\a\cpp_projects\sdk-cpp-workspace\lib\aws-crt-cpp\cmake
But how to tell about it to CMake?
I'm running CentOS 7.8 via dual-boot on a 64-bit 2013 Mac with a GT 650M GPU. I'm using CMake 3.17, CUDA 10.0, and GCC 4.8.5. All CUDA samples have been tested and work fine, and I'm able to compile other standard C++ code perfectly.
I've reduced my full project to a simple test case as follows, where the CMakeLists file is:
CMAKE_MINIMUM_REQUIRED(VERSION 3.8)
PROJECT(test LANGUAGES CUDA CXX C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
MESSAGE(STATUS "Setting to Release mode")
SET(CMAKE_BUILD_TYPE "Release")
# Set CUDA flags
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -arch=sm_30 -rdc=true")
# Set flags
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -ffast-math")
MESSAGE(STATUS "Setting g++ flags for Release configuration")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") ## Optimize
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s ") ## Strip binary
ADD_SUBDIRECTORY( src )
In the /src folder I have another CMake file to gather the source files:
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR} )
SET(test_SRCS
rsmain.cu
SGP4.cu
SGP4.cuh
)
function(my_add_executable TargetName)
set(Files ${ARGV})
list(REMOVE_AT Files 0)
add_executable(${TargetName} ${Files})
set_target_properties(${TargetName} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_SOURCE_DIR}/build")
endfunction()
my_add_executable(test ${test_SRCS})
INSTALL( TARGETS test DESTINATION bin)
As shown, there are three main source files - both SGP4.cu and SGP4.cuh are empty, while rsmain.cu is simply:
/// Main function
int main(int argc, char *argv[])
{
return 0;
}
When trying to build, I get the following output:
[me#localhost build]$ cmake3 ..
-- The CUDA compiler identification is NVIDIA 10.0.130
-- The CXX compiler identification is GNU 4.8.5
-- The C compiler identification is GNU 4.8.5
-- Check for working CUDA compiler: /usr/local/cuda-10.0/bin/nvcc
-- Check for working CUDA compiler: /usr/local/cuda-10.0/bin/nvcc - works
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- No build configuration specified, defaulting to Release
-- Setting general compiler flags for detected compiler: gnu-g++
-- Setting g++ flags for Release configuration
-- Configuring done
-- Generating done
-- Build files have been written to: /home/me/Documents/test/build
[me#localhost build]$ make
/usr/bin/cmake3 -S/home/me/Documents/test -B/home/me/Documents/test/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake3 -E cmake_progress_start /home/me/Documents/test/build/CMakeFiles /home/me/Documents/test/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/me/Documents/test/build'
make -f src/CMakeFiles/test.dir/build.make src/CMakeFiles/test.dir/depend
make[2]: Entering directory `/home/me/Documents/test/build'
cd /home/me/Documents/test/build && /usr/bin/cmake3 -E cmake_depends "Unix Makefiles" /home/me/Documents/test /home/me/Documents/test/src /home/me/Documents/test/build /home/me/Documents/test/build/src /home/me/Documents/test/build/src/CMakeFiles/test.dir/DependInfo.cmake --color=
Scanning dependencies of target test
make[2]: Leaving directory `/home/me/Documents/test/build'
make -f src/CMakeFiles/test.dir/build.make src/CMakeFiles/test.dir/build
make[2]: Entering directory `/home/me/Documents/test/build'
[ 33%] Building CUDA object src/CMakeFiles/test.dir/rsmain.cu.o
cd /home/me/Documents/test/build/src && /usr/local/cuda-10.0/bin/nvcc -I/home/me/Documents/test/src -I/home/me/Documents/test/build/src -I/home/me/Documents/test/build -arch=sm_30 -rdc=true -O3 -DNDEBUG -std=c++03 -x cu -c /home/me/Documents/test/src/rsmain.cu -o CMakeFiles/test.dir/rsmain.cu.o
[ 66%] Building CUDA object src/CMakeFiles/test.dir/SGP4.cu.o
cd /home/me/Documents/test/build/src && /usr/local/cuda-10.0/bin/nvcc -I/home/me/Documents/test/src -I/home/me/Documents/test/build/src -I/home/me/Documents/test/build -arch=sm_30 -rdc=true -O3 -DNDEBUG -std=c++03 -x cu -c /home/me/Documents/test/src/SGP4.cu -o CMakeFiles/test.dir/SGP4.cu.o
[100%] Linking CUDA executable ../test
cd /home/me/Documents/test/build/src && /usr/bin/cmake3 -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/usr/bin/g++ -s CMakeFiles/test.dir/rsmain.cu.o CMakeFiles/test.dir/SGP4.cu.o -o ../test -lcudadevrt -lcudart_static -L"/usr/local/cuda-10.0/targets/x86_64-linux/lib/stubs" -L"/usr/local/cuda-10.0/targets/x86_64-linux/lib" -lcudadevrt -lcudart_static -lrt -lpthread -ldl
CMakeFiles/test.dir/rsmain.cu.o: In function `__sti____cudaRegisterAll()':
tmpxft_00004eed_00000000-5_rsmain.cudafe1.cpp:(.text.startup+0x25): undefined reference to `__cudaRegisterLinkedBinary_41_tmpxft_00004eed_00000000_6_rsmain_cpp1_ii_main'
CMakeFiles/test.dir/SGP4.cu.o: In function `__sti____cudaRegisterAll()':
tmpxft_00004f02_00000000-5_SGP4.cudafe1.cpp:(.text.startup+0x15): undefined reference to `__cudaRegisterLinkedBinary_39_tmpxft_00004f02_00000000_6_SGP4_cpp1_ii_71922fcb'
collect2: error: ld returned 1 exit status
make[2]: *** [test] Error 1
make[2]: Leaving directory `/home/me/Documents/test/build'
make[1]: *** [src/CMakeFiles/test.dir/all] Error 2
make[1]: Leaving directory `/home/me/Documents/test/build'
make: *** [all] Error 2
Could anyone please explain what this 'cudaRegisterLinkedBinary' stuff is all about? I've tried a bunch of things in trying to solve it but nothing has worked so far. Is there an issue with any of the package versions? A problem in CMakeLists? Compatibility issues with CUDA and my hardware?
It's worth noting that the full code compiled and ran perfectly when I tested it on a HPC server (also running CentOS 7 and Cuda 10.0) - but on my personal PC it fails at the linking step. I've even confirmed that the .bashrc files are the same across both installations, but it hasn't fixed anything. I'm also currently able to compile NVIDIA's OptiX software (which also uses CUDA) without any problems.
Any advice would be appreciated. Please let me know if I missed any required details.
EDIT: Answer added below. Resolved.
Finally sorted this out. This was my main CMakeLists file:
CMAKE_MINIMUM_REQUIRED(VERSION 3.8)
PROJECT(test LANGUAGES C CXX CUDA)
SET(CMAKE_BUILD_TYPE "Release")
# Set CUDA flags
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -arch=sm_30 -rdc=true")
# Set flags
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -ffast-math -O3")
MESSAGE(STATUS "Setting g++ flags for Release configuration")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s") ## Strip binary
ADD_SUBDIRECTORY( src )
And in the /src CMakeLists file, I had to make the change:
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR} )
SET(test_SRCS
rsmain.cu
SGP4.cu
SGP4.cuh
)
function(my_add_executable TargetName)
set(Files ${ARGV})
list(REMOVE_AT Files 0)
add_executable(${TargetName} ${Files})
set_target_properties(${TargetName} PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON
RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_SOURCE_DIR}/build")
endfunction()
my_add_executable(test ${test_SRCS})
INSTALL( TARGETS test DESTINATION bin)
Setting CUDA_RESOLVE_DEVICE_SYMBOLS to ON is the change. In my main project, I also had to repeat this for every target involving any CUDA files. Everything compiles and runs perfectly now.
I'm trying to compile a simple program with SFML (basic helloworld but with a big green circle). I'm supposed to build the program on Linux for Linux and Windows. When I compile for Linux the program compile and run perfectly, however when I try to compile the Windows Executable, I get the following error:
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lsfml-graphics
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lsfml-window
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lsfml-system
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/BrickShooter.dir/build.make:215: BrickShooter.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/BrickShooter.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I tried to change the setting in my Cmake without success (add_library, include_directories) but so far nothing worked.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(BrickShooter)
set(CMAKE_CXX_STANDARD 14)
add_executable(BrickShooter main.cpp GameObjects/Block.cpp GameObjects/Block.h GameObjects/Player.cpp GameObjects/Player.h GameObjects/Shoot.cpp GameObjects/Shoot.h SysObjects/Game.cpp SysObjects/Game.h SysObjects/Store.cpp SysObjects/Store.h SysObjects/Menu.cpp SysObjects/Menu.h SysObjects/Backup.cpp SysObjects/Backup.h SysObjects/SysGame.cpp SysObjects/SysGame.h)
target_link_libraries (BrickShooter sfml-graphics sfml-window sfml-system)
And my toolchain for Windows:
# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
# *) install cross compiler: `sudo apt-get install mingw-w64`
# *) cd build
# *) cmake -DCMAKE_TOOLCHAIN_FILE=~/windows.cmake ..
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
# cross compilers to use for C, C++ and Fortran
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
# target environment on the build host system
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})
# modify default behavior of FIND_XXX() commands
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(SFML_INCLUDE_DIR "gitlab_ci/SFML-2.5.1-WIN/include")
include_directories(gitlab_ci/SFML-2.5.1-WIN/include)
set(SFML_LIBRARY_DIR "gitlab_ci/SFML-2.5.1-WIN/lib")
set(SFML_DIR "gitlab_ci/SFML-2.5.1-WIN/lib/cmake/SFML")
I'm using the following command to manually build the program:
cmake gitlab_ci/SFML-2.5.1-WIN/ -DCMAKE_TOOLCHAIN_FILE=gitlab_ci/windows.cmake CMakeLists.txt && make
Where gitlab_ci/SFML-2.5.1-WIN is where I stored the includes, libs, etc for Windows. I'm on Ubuntu 20.04 LTS and I have installed everything I needed with the following commands:
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential libsfml-dev cmake doxygen mingw-w64 curl unzip
curl -O https://www.sfml-dev.org/files/SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
unzip SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
mv SFML-2.5.1 gitlab_ci/SFML-2.5.1-WIN
rm SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
Thanks to #tsyvarev here is the solution that worked for me:
I added this line to my toolchain for Windows
link_directories(gitlab_ci/SFML-2.5.1-WIN/lib)
The final result is:
# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
# *) install cross compiler: `sudo apt-get install mingw-w64`
# *) cd build
# *) cmake -DCMAKE_TOOLCHAIN_FILE=~/windows.cmake ..
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
# cross compilers to use for C, C++ and Fortran
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
# target environment on the build host system
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})
# modify default behavior of FIND_XXX() commands
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(SFML_INCLUDE_DIR "gitlab_ci/SFML-2.5.1-WIN/include")
include_directories(gitlab_ci/SFML-2.5.1-WIN/include)
set(SFML_LIBRARY_DIR "gitlab_ci/SFML-2.5.1-WIN/lib")
link_directories(gitlab_ci/SFML-2.5.1-WIN/lib)
set(SFML_DIR "gitlab_ci/SFML-2.5.1-WIN/lib/cmake/SFML")
and this give the following output:
cmake gitlab_ci/SFML-2.5.1-WIN/ -DCMAKE_TOOLCHAIN_FILE=gitlab_ci/windows.cmake CMakeLists.txt && make
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/shared/school/Ynov/Home_Work/uf_dev_log_13/repo_gitlab
Scanning dependencies of target BrickShooter
[ 10%] Building CXX object CMakeFiles/BrickShooter.dir/main.cpp.obj
[ 20%] Building CXX object CMakeFiles/BrickShooter.dir/GameObjects/Block.cpp.obj
[ 30%] Building CXX object CMakeFiles/BrickShooter.dir/GameObjects/Player.cpp.obj
[ 40%] Building CXX object CMakeFiles/BrickShooter.dir/GameObjects/Shoot.cpp.obj
[ 50%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Game.cpp.obj
[ 60%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Store.cpp.obj
[ 70%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Menu.cpp.obj
[ 80%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Backup.cpp.obj
[ 90%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/SysGame.cpp.obj
[100%] Linking CXX executable BrickShooter.exe
[100%] Built target BrickShooter
Here is the link to the CMake docs about link_directories()
I've been trying to compile a simple OpenMP program using AppleClang on Mac OS X 10.14.5 Mojave with the CLion IDE.
main.cpp:
#include <omp.h>
#include <iostream>
int main() {
std::cout << omp_get_max_threads() << std::endl;
return 1;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(OpenMPTest)
set(CMAKE_CXX_STANDARD 17)
add_executable(OpenMPTest main.cpp)
find_package(OpenMP)
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}")
endif()
CMake output:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/bully/CLionProjects/OpenMPTest
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_C: -Xclang -fopenmp (found version "3.1")
-- Found OpenMP_CXX: -Xclang -fopenmp (found version "3.1")
-- Found OpenMP: TRUE (found version "3.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/bully/CLionProjects/OpenMPTest/cmake-build-debug
When I build the project, I receive a linker error:
====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/bully/CLionProjects/OpenMPTest/cmake-build-debug --target all -- -j 2
Scanning dependencies of target OpenMPTest
[ 50%] Building CXX object CMakeFiles/OpenMPTest.dir/main.cpp.o
[100%] Linking CXX executable OpenMPTest
Undefined symbols for architecture x86_64:
"_omp_get_max_threads", 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[2]: *** [OpenMPTest] Error 1
make[1]: *** [CMakeFiles/OpenMPTest.dir/all] Error 2
make: *** [all] Error 2
How come this does not work? From the command line I can run clang++ main.cpp -lomp successfully after installing the library headers with open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg.
message(STATUS "Linker flags:" "${OpenMP_EXE_LINKER_FLAGS}") prints:
-- Linker flags:
If I replace the CMAKE_EXE_LINKER_FLAGS setter with set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lomp") compilation with linking works. Why do I have to specify this manually? What's the preferred way of getting this to work in a platform-independent way? -fopenmp yields clang: error: unsupported option '-fopenmp'. gcc and MSVC on Linux and Windows respectively played nice with the CMake OpenMP configuration but Mac OS X does not.
As commented by Tsyvarev, the solution is to use the "updated" way of including OpenMP in CMake:
cmake_minimum_required(VERSION 3.14)
project(OpenMPTest)
set(CMAKE_CXX_STANDARD 17)
add_executable(OpenMPTest main.cpp)
find_package(OpenMP REQUIRED) # Find the package
target_link_libraries(${PROJECT_NAME} ${OpenMP_CXX_LIBRARIES}) # Link against it for C++
This compiled on Windows, Ubuntu and Mac OS X using their platform's default compilers respectively.
The accepted answer here is not recommended even though it also has the most upvotes.