CMake not generating compile_commands.json - c++

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

Related

VSCode cannot find package but CLion can (CMake)

I was wondering what the difference is between using cmake and (compile_commands and compile_flags) in VSCode? I'm running into an issue where even though I've got my CMake set up correctly (builds just fine on Clion), I run into issues when trying to use the exact same code in VSCode.
I've got the offending line below
#include "palisade.h"
where palisade is installed in "/usr/local/include/palisade" and my CMake explicitly looks for it and finds it
find_package(Palisade)
I've also generated the file compile_commands.json
via cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1
but I'm still running into issues of the library not being found
Probably it will helps.
In main CMakeLists.txt add line
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
and in c_cpp_properties.json
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
This will allow InteliSence to use the dependencies created by cmake.

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

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!

Build uWebSockets on Windows 10 with CMake

I want to use uWebSockets(UWS) in my C++ project to transfer images over the network. The setup will be running on multiple operating systems, so the best way of creating the build files seemed like using CMake.
However, I am new to CMake and having a hard time building UWS. I don't understand how to start working with the official repository on Windows 10, so I found another repository that includes a CMakeFiles.txt file and some of the dependencies (openssl, zlib, libuv, but it doesn't include uSockets for some reason). The root CMakeFiles.txt includes:
[...]
find_package(OpenSSL REQUIRED)
find_package(ZLIB REQUIRED)
find_path(LIBUV_INCLUDE_DIR uv.h)
find_library(LIBUV_LIBRARY NAMES uv uv1)
[...]
It looks straightforward, but when I try to run mkdir build && cd build; cmake .., it cannot find OpenSSL. Here is the error message it spits out:
[...]
[cmake] Could not find a package configuration file provided by "OpenSSL" with any
[cmake] of the following names:
[cmake]
[cmake] OpenSSLConfig.cmake
[cmake] openssl-config.cmake
[...]
The above error message suggests that I need to set up a config file for each of these libraries. Yet, if I understand the find_package docs correctly, the command itself searches the library in various locations under the root folder. What kind of a folder structure does the find_package need in order to work?
More generally, am I wasting my time with this alternative repo? Is there a better way of using UWS with Windows 10? The official repo has a question about how to use it on Windows but I don't understand how that's an answer to the question. It only points to this page where it says any specific build systems will not officially be supported.
Any help would be appreciated.
Importing dependencies with add_subdirectory seems like a good way around this. When I ran cmake, I was receiving LNK2019 Error. I realized the following code snippet I found online was causing the problem, and the system works when I delete it.
if(MSVC)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrt.lib")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS")
endif()

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

How to change where CMakeLists.txt looks for Boost Libraries Ubuntu

I was using Boost 1.54.0 and it was located in "/usr/include". We blew that away and installed Boost 1.57.0. It got installed in "/usr/local/include".
Now, my CLion project, which uses CMake cannot find the Boost library. Here is my CMakeLists.txt file:
And here are my errors:
I have no idea how to make CMake look in the correct place for Boost.
According to the FindBoost documentation (http://www.cmake.org/cmake/help/v3.1/module/FindBoost.html), you can set a CMake variable BOOST_ROOT to give CMake a hint about where to look.
In your CMakeLists.txt file, you can add the following before the find_package(Boost...) line:
set(BOOST_ROOT /usr/local)
Update:
I agree with the comments that putting machine specific configuration parameters directly in CMakeLists.txt is not best practice.
As an alternative to directly setting this variable, you can pass options like this to the cmake process in CLion by doing the following:
Navigate to File -> Settings... -> Build, Execution, and Deployment -> CMake. Under Generation, add -DBOOST_ROOT=/usr/local to CMake options.