CMake not detecting CXX but detecting CPP files - c++

I'm trying to learn CMake from http://www.cmake.org/cmake/help/cmake_tutorial.html and running into trouble with the first step itself of running a simple file tutorial.cpp.
The issue is that when I have this command in my CMakeLists.txt file
add_executable(Tutorial tutorial.cpp)
it builds fine.
However, when I change it to
add_executable(Tutorial tutorial.cxx)
It gives the following error
-- Configuring done
CMake Error at src/CMakeLists.txt:6 (add_executable):
Cannot find source file:
tutorial.cxx
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
-- Build files have been written to: /home/vaisagh/workspace/Test/build
My directory structure is like this:
.
├── build
├── CMakeLists.txt
└── src
├── CMakeLists.txt
└── tutorial.cpp
2 directories, 3 files
CMakeLists.txt
#Specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.8.11)
#Name your project here
project(Tutorial)
add_subdirectory(src)
src/CMakeLists.txt
#Specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.8.11)
add_executable(Tutorial tutorial.cxx)

Tried extensions .c .C... Indicates that CMake tried to search for tutorial.cxx.c, tutorial.cxx.C, etc.
The source filename given to add_executable must match the actual filename on disc.
Rename tutorial.cpp to tutorial.cxx -or-
Change add_executable(Tutorial tutorial.cxx) to add_executable(Tutorial tutorial.cpp)

Related

Creating c++ project using cmake and automatically adding source and header files

I'm new to cmake and im trying to create a project for simulating a few libraries im writing to run on arduino. This means the directory structure of the libraries cant be changed but i can add a cmakelists file but would rather not. I need some help writing the CMakeLists file.
The simulator directory structure is:
include/
lib/
src/
CMakeLists.txt
Each library im writing must have this file structure:
TestLib/
include/VeryGoodLib.hpp
include/VeryGoodLib/sometool.hpp
src/*.cpp //All the source files
Each library is placed into the main projects lib/ directory and im hoping to make get cmake to "glob" all the libraries inside lib/ and automatically add them to the main project while also making them show up when I write #include. In this cade i would see the VeryGoodLib/ folder and VeryGoodLib.hpp header.
My current CMakeLists file is:
cmake_minimum_required(VERSION 3.0.0)
project(EasyVTOL-Sim VERSION 0.1.0)
file(GLOB_RECURSE SRC_FILES src/*.cpp)
add_executable(EasyVTOL-Sim ${SRC_FILES})
target_include_directories(EasyVTOL-Sim PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Any simple way of globing the files and including them this way?

Execute output of cmake target as dependency for another

I have the following directory structure:
.
├── CMakeLists.txt
├── generator
│   ├── CMakeLists.txt
│   └── main.cpp
├── include
└── src
├── CMakeLists.txt
└── mylib.cpp
I would like to build generator, then use generator to generate a source file that will be used to build mylib. I tried this:
generator/CMakeLists.txt:
add_executable(gen main.cpp)
add_custom_command(
OUTPUT
${CMAKE_BINARY_DIR}/generated.cpp
DEPENDS
gen
COMMAND
${CMAKE_BINARY_DIR}/gen -d /tmp
VERBATIM
)
src/CMakeLists.txt:
add_library(py-cppast
${CMAKE_BINARY_DIR}/generated.cpp
mylib.cpp)
CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.2)
project(my-project)
add_subdirectory(generator)
add_subdirectory(src)
However, the command is not executed. Instead, I just get the error:
CMake Error at src/CMakeLists.txt:2 (add_library):
Cannot find source file:
/home/....(the cmake binary dir)..../generated.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
How can I tell cmake to execute the program I'm building with it? Is this even possible in a single build?
The problem comes from the fact that you are generating the file generated.cpp in one directory and then trying to add it to a target defined in a different directory. CMake only supports adding generated files to targets defined in the same directory scope. The add_custom_command() documentation explicitly mentions this restriction.
You probably want to move the generation of generated.cpp into the src directory. You should also use just the gen target name to refer to the executable to run, not ${CMAKE_BINARY_DIR}/gen which is not going to be correct with all CMake generator types. It would also be better style to use the current binary directory rather than the top level binary directory as the output dir. Your src/CMakeLists.txt should look something like this:
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated.cpp
DEPENDS gen
COMMAND gen -d /tmp
VERBATIM
)
add_library(py-cppast
${CMAKE_CURRENT_BINARY_DIR}/generated.cpp
mylib.cpp
)
CMake will automatically substitute the location of the gen target for you, even though it was defined in a different directory. There are some further subtleties to be aware of when using generated sources, particularly relating to dependencies. You may find this article helpful to fill in some gaps.

cpplint.py & cmake: how to specify include files

Assume I have a project of the following directory structure:
myproject
├── .git [...]
├── CMakeLists.txt
└── src
├── CMakeLists.txt
├── foo.cc
└── foo.h
If in src/foo.cc I include the header file like #include "foo.h" and then run Google's cpplint.py on it, it complains with
src/foo.cc:8: Include the directory when naming .h files [build/include] [4]
So I include it as #include "./foo.h". Now I get another complaint:
src/foo.cc:8: src/foo.cc should include its header file src/foo.h [build/include] [5]
However, if I include it as #include "src/foo.h", the compiler won't find it, with my current setup of CMake. This is how my two CMakeLists.txt files look like:
CMakeLists.txt:
project(myproject)
add_subdirectory(src)
src/CMakeLists.txt:
set(SRCS foo.cc)
add_executable(foo ${SRCS})
Is the way I'm using CMake somehow fundamentally wrong? Should I remove the src/CMakeLists.txt file entirely, and specify all source files in the base CMakeLists.txt with their full path?
Or should I simply ignore cpplint's complaints, as they don't really fit to how CMake projects are to be set up?
Add include_directories(${CMAKE_SOURCE_DIR}) in your top level CMakeLists.txt, like Wander suggested:
project(myproject)
include_directories(${CMAKE_SOURCE_DIR})
add_subdirectory(src)

create one CmakeList.txt file for multiple subdirectories each containg its own src(.cpp files) and include(.h files)

How to write one Cmake file that compile all my sub-directories each with its own executable?
/Project
Cmakelist.txt
/project 1
/src
.cpp
/include
.h
/project 2
/src
.cpp
/include
.h
Include this in every CmakeList.txt subfolder
cmake_minimum_required(VERSION 3.5.2)
project(project1)
set(SRC "src/name.cpp" "include/name2.h")
add_executable(project1 ${SRC})
target_include_directories (project1 PRIVATE include)
And this in the CmakeList.txt root directory
cmake_minimum_required(VERSION 3.5.2)
project(project)
add_subdirectory(project1)
add_subdirectory(project2)
...add more if you want
you can also delete the header files from set, it will also work

cmake: how to include/compile files out of the project directory?

I have this directory structure:
projects/
project1/
src/
main.cpp
CMakeLists.txt
project2/
src/
file1.h
file1.cpp
test1.cpp
The top level projects directory cannot be considered a top level project, but just a collection of unrelated projects, so I would not put a CMakeLists.txt file at that level.
I want project1 to include files from project2 without specifying the full path, e.g.:
// main.cpp
#include "file1.h"
And I also want implementation files of project2 to be built in project1.
I need project2 not to be a library, but just use its files like if they were part of project1.
I am using cmake, and this is the CMakeLists.txt file I wrote (it does not work):
cmake_minimum_required(VERSION 2.8)
project(project1)
add_subdirectory(src)
add_subdirectory(../../project2/src)
Even specifying the full path to project2 does not work: ${CMAKE_CURRENT_SOURCE_DIR)/../../project2/src
I get "fatal error: file1.h: no such file or directory" from make.
To use external include files, this works:
include_directories