How do I designate which compiler is called when running cmake + make? - c++

I'm trying to compile a c++ project with cmake and make on OSX but it looks like make is using CXX or clang when I want to use g++ (gcc) so I can follow the answer here to tell the compiler where to find header files (#includes) for tbb used in the project: Need help getting intel TBB working?
brew list shows that I have up to date versions of cmake, make, gcc, and swig installed.
Here's the project I'm trying to compile for reference: https://github.com/nmoehrle/mvs-texturing/blob/master/README.md

I came across this related answer and was able to get the project working! MacOS, CMake and OpenMP
I updated the cmakelists.txt with the following commands to set llvm as the compiler. Note, I needed to update the llvm file path to match the version number I have installed.
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/5.0.1/lib")
set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/5.0.1/include")
Then I was also having issues with OpenMP so I added this section to configure the OpenMP include directories and link directories.
if (OPENMP_FOUND)
include_directories("${OPENMP_INCLUDES}")
link_directories("${OPENMP_LIBRARIES}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif(OPENMP_FOUND)
I'm a novice dev and wish I had more insight into why this worked but it fixed my issues!

Related

Compiling a 32-bit C++ program in CLion on MacOS

I having trouble with my CMake file in CLion running on Mac OSX 10.14. Currently, I have the following for a 32-bit program I copied from a Linux server using a built "supplied.o" file from an instructor. I wanted to work in CLion and wondering if it is possible to add to CLion to work outside of the Linux server?
cmake_minimum_required(VERSION 3.14)
project(app)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
add_executable(app
dlist.cpp
dlist.h
main.cpp
supplied.o)
set_target_properties(app PROPERTIES LINKER_LANGUAGE CXX )
set_target_properties(app PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
I tried poking around here to see if I could figure it out, but it's possible I don't have a 32-bit G++ library (is that i386?) and not quite sure how to go about installing that. I have homebrew and feel comfortable enough with Linux to just work there, but I enjoy CLion for getting quick ideas out. Thanks for any help anyone can provide!

link qt5 libraries automatically pass extra fPIC flag to nvcc compiler by cmake cause error

I have a project based on cuda10.1, qt5. And I use cmake to manage the compilation. However the same cmake script work fine in Windows, but in Linux, I got error "Unknown option 'fPIC'" when compiling .cu files. I look into makefile, then find there is a extra error flag "-fPIC" passed to nvcc compiler. Spending hours of testing, I find linking qt5 libraries will automatically pass extra fPIC flag which causes this error. I don't know this is bug or feature of cmake?
cmake_minimum_required(VERSION 3.10)
project(PyPhysLeo LANGUAGES CXX CUDA)
find_package(Qt5 COMPONENTS Core Widgets)
if(Qt5_FOUND)
message("Find QT5")
link_libraries(Qt5::Widgets Qt5::Core Qt5::Gui)
endif()
set(CMAKE_CXX_STANDARD 14 CACHE STRING "CXX STANDARD VERSION 11,14,17")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CUDA_SEPARABLE_COMPILATION ON CACHE BOOL "DEFAULT SET SEPERABLE COMPILATION MODE")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -arch=sm_60")
include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE})
add_executable(test test.cu)
if you try above cmake script with any sample cuda code, you will get "nvcc fatal" error because it doesn't recognize fPIC flag.
It is a problem with qt5.cmake. Thanks to Robert Crovella for commenting (see above) about the related information gitlab.kitware.com/cmake/cmake/issues/16915. There is no perfect solution so far. But...
One solution, if you can separate your project into a library and an executable sub-projects, then you can cleanly avoid the issue altogether. Try to put all the .cu files in a library sub-project and only link qt5 in an executable sub-project. This is one way to avoid this problem.
Otherwise, you can try to modify the variable defined by qt5.cmake, for that solution please refer to cmake issue 16915.

LibTorch with CMake via Eclipse in Windows:Terminated exit value 390

I used cmake4eclipse to build torch C++ version 1.0 stable in Windows 10. Basically, I have the following CMakeLists.txt to build the mnist example:
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(mnist)
set(CMAKE_PREFIX_PATH "C:/rl/libtorch/share/cmake/Torch")
set(Torch_DIR "C:/rl/libtorch")
find_package(Torch REQUIRED)
option(DOWNLOAD_MNIST "Download the MNIST dataset from the internet" ON)
if (DOWNLOAD_MNIST)
message(STATUS "Downloading MNIST dataset")
execute_process(
COMMAND python ${CMAKE_CURRENT_LIST_DIR}/download_mnist.py
-d ${CMAKE_BINARY_DIR}/data
ERROR_VARIABLE DOWNLOAD_ERROR)
if (DOWNLOAD_ERROR)
message(FATAL_ERROR "Error downloading MNIST dataset: ${DOWNLOAD_ERROR}")
endif()
endif()
set(CMAKE_BUILD_TYPE Debug)
add_executable(mnist mnist.cpp)
target_compile_features(mnist PUBLIC cxx_range_for)
set_property(TARGET mnist PROPERTY CXX_STANDARD 14)
target_link_libraries(mnist ${TORCH_LIBRARIES})
Then, I load this along with the mnist.cpp and download_mnist.py files in a folder and start a project in eclipse IDE for C/C++, version 2018-09 (4.9.0). In project_properties->C/C++ Build->Tool Chain Editor, I set CMake Builder (GNU Make) and select MinGW GCC. Then, in project_properties->C/C++ General->Preprocessor Include Paths Macros etc.->Providers I select CMAKE_EXPORT_COMPILE_COMMANDS Parser [Shared] and move it up, as it is explained here.
Then, I can compile the mnist project without any error. But, when I run it get <terminated> (exit value 390) a.exe [some address]. I tried to debug this code to find out the problem, but I cannot see the debug screen, and instead I get:
Running the debug mode to the end results in a same error.
I can run mnist.cpp in Linux without any problem, though I use cmake -G "Eclipse CDT4 - Unix Makefiles" ./ to create a eclipse project. I did not know how I can use cmake -G "Eclipse CDT4 - Unix Makefiles" ./ in Windows and I used cmake4eclipse and I believe I have missed a step in dealing with the CMakeLists.txt file in windows. I appreciate any help or comments.
Thanks,
Afshin
I asked same question in torch git, and today I got an answer for that. It seems that for now, we will not be able to run Libtorch through Eclipse with MinGw. Here is the answer that I got from torch git page:
"I don't think you could build that with MinGW because the code is written in c++ and MinGW is not abi-compatible with MSVC. So I think you may need to compile with MSVC. And also in MSVC, the configuration debug and release could not be mixed. So you will have to choose Release as we only provide library with the Release configuration."
See more details in:
https://github.com/pytorch/pytorch/issues/15711

Linking libc++ to CMake project on Linux

I want to use libc++ together with clang on Arch Linux in CMake project. I installed libc++ and added following lines to CMakeLists.txt as said on LLVM site in Linux section of "Using libc++ in your programs":
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "-lc++abi")
I have tried just "++abi" in linker's flags, but it didn't help. I need some help in figuring out what i should write in my CMakeLists.txt.
Don't forget to set the compiler to clang++:
set(CMAKE_CXX_COMPILER "clang++")
Also, purge the cmake generated files (delete the folder CMakeFiles and CMakeCache.txt).
Depending on your system, it might also help to set
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
The "proper" way to do this in CMake at the moment, until a specific base feature is added to switch standard libraries that is, is to use a toolchain file.
In that toolchain file you specify the compiler etc similarly to the other answers here.
BUT what's great about toolchains is they can be swapped out quickly either on the commandline (using -DCMAKE_TOOLCHAIN_FILE=path/to/file) OR in VSCode with CMakeTools extension installed, and probably other editors too.
But having to hand code your own toolchain files is yet another obscure chore! No fun!
Luckily, I stumbled upon this github that maintains a suite of them so you don't have to write them from scratch! Should be a lot less likely to get them wrong.
https://github.com/ruslo/polly

CMake not generating compile_commands.json

I'm new to CMake and I'm trying to create the compile_commands.json file to use with clang, but I'm having some difficulties generating the file and I'm not sure why. I've been able to use cmake to compile the binary person that I have below, but after that was successful I've been unable to get it to output the compile commands.
I've also tried doing the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON flag, but that didn't work either. So far there's been no errors, but also no output.
Here's what my CMakeLists.txt file looks like:
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(person Person.cc Pirate.cc main.cc)
This ended up being an issue with using an old version of CMake. I ended up installing the newest version and it worked as expected.
According to Clang docs
"Currently CMake (since 2.8.5) supports generation of compilation databases for Unix Makefile builds (Ninja builds in the works) with the option CMAKE_EXPORT_COMPILE_COMMANDS."
I also encountered the same problem as you.
According to CMake doc
This option (CMAKE_EXPORT_COMPILE_COMMANDS) is implemented only by Makefile Generators and the Ninja. It is ignored on other generators.
Thus, there is no solution to generate compile_commands.json file when using MSVC.
I had the same problem, compile_commands.json was not generated with cmake, version 3.16.0.
It was generated when I used the Ninja generator, but not Unix Makefiles.
That discussion gave me the fix:
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # does not produce the json file
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") # works