My project structure:
/external-source-generating-tool
/external-source-generating-tool/CMakeLists.txt
/external-source-generating-tool/*.cpp
/src
/src/CMakeLists.txt
/src/*.cpp
/CMakeLists.txt
CMakeLists.txt:
add_subdirectory(external-source-generating-tool)
add_subdirectory(src)
/src/CMakeLists.txt:
add_custom_command(OUTPUT generated-source.cpp
COMMAND external-source-generating-tool -o generated-source.cpp
MAIN_DEPENDENCY external-source-generating-tool
COMMENT "Generating...")
add_executable(my-app source1.cpp generated-source.cpp)
The problems is /src/CMakeLists.txt cannot find external-source-generating-tool:
Error:Cannot find source file:
external-source-generating-tool
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
How correctly call external-source-generating-tool from /src/CMakeLists.txt?
The problem was with the MAIN_DEPENDENCY external-source-generating-tool parameter of add_custom_command. I replaced it by DEPENDS external-source-generating-tool:
add_custom_command(OUTPUT generated-source.cpp
COMMAND external-source-generating-tool -o generated-source.cpp
DEPENDS external-source-generating-tool
COMMENT "Generating...")
And now all works fine.
Related
I am really new to C++ and I am a bit confused.
I am trying to add this lib to my project (https://github.com/mrtazz/restclient-cpp).
I have installed it using Homebrew
brew tap mrtazz/oss
brew install restclient-cpp
then I tried adding the library to my CMakeLists by including and linking the Homebrew install directories.
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(POS)
set(CMAKE_CXX_STANDARD 14)
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
add_library(
restclient-cpp STATIC
connection.h
helpers.h
restclient.h
version.h
)
add_executable(POS main.cpp program.cpp program.h programs/find.cpp programs/find.h tools/db.cpp tools/db.h)
target_link_libraries(POS PUBLIC restclient-cpp)
then I get this error...
CMake Error at CMakeLists.txt:16 (add_library):
Cannot find source file:
connection.h
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx
CMake Error at CMakeLists.txt:16 (add_library):
No SOURCES given to target: restclient-cpp
I know something is wrong with the directories but I just cannot figure it out, I would greatly appreciate as much information as possible. I'm just trying to have some fun with this and I cannot figure out why I cannot add this simple library to my build.
Thank you.
You are trying to add header files to the add_library command. Those files need to be in a directory that you include via include_directory. You also should not put header files into the add_executable command.
To link an existing library you can call target_link_libraries.
example:
include_directories(${MY_INCLUDE_DIRS})
add_executable(main source.cpp)
target_link_libraries(main extlib)
It's probably best to find a simple CMake setup and try to use it as template.
I've using cmake to build my google rpc sample code. I've got
examples.proto
client.cpp
server.cpp
I use protoc command to build out .cc/.h files for both protobuf and grpc, CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
add_definitions(-std=c++11)
include_directories(.)
add_custom_target(protoFile
PRE_BUILD
COMMAND protoc --cpp_out=./ examples.proto
)
add_custom_target(protoSource
PRE_BUILD
COMMAND protoc --grpc_out=./ --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin examples.proto
)
add_dependencies(protoSource protoFile)
add_library(protoCpp SHARED examples.grpc.pb.cc examples.pb.cc)
add_dependencies(protoCpp protoSource)
link_libraries(protoCpp protobuf grpc grpc++)
add_executable(client client.cpp)
add_executable(server server.cpp)
And then:
cmake .
-- Configuring done
CMake Error at CMakeLists.txt:15 (add_library):
Cannot find source file:
examples.grpc.pb.cc
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error: CMake can not determine linker language for target: protoCpp
CMake Error: Cannot determine link language for target "protoCpp".
-- Generating done
Seems my "add_custom_target()" was not executed, and "ls" didn't show any files I expected. So how to make them executed? I already added "protoSource as dependency for "protoCpp" library, but didn't work.
How to solve this?
Use add_custom_target after add_executable, and the first parameter should be a target created by add_executable, in your example "client" or "server". You can't use PRE_BUILD in add_custom_target, but I'm not sure;
See also: add_custom_target documentation.
I have a library with several source files under several directories. I want to write a cmake in a way that it add all of them to project without asking to writ them separately.
I am using this line in my cmake:
FILE(GLOB_RECURSE ALL_Lib_CPP_SRCS src/Library/ *.cpp)
add_library(MyLibrary STATIC ALL_Lib_CPP_SRCS)
but generate msvc project doesn't have all files included and I am getting this message when I am running cmake:
CMake Error at CMakeLists.txt:49 (add_library):
Cannot find source file:
ALL_Lib_CPP_SRCS
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx
What is the problem with this cmake?
It should be:
FILE(GLOB_RECURSE ALL_Lib_CPP_SRCS "src/Library/*.cpp")
add_library(MyLibrary STATIC ${ALL_Lib_CPP_SRCS})
In my opinion, it's better that you defined manually the sources:
set (_SOURCES source3.cpp source2.cpp source1.cpp main.cpp)
# now use ${_SOURCES}
this way, you can know the exact order of compilation...sometimes the order is important
The correct syntax according to the manual is:
file(GLOB_RECURSE variable [RELATIVE path] [FOLLOW_SYMLINKS] [globbing expressions]...)
I suspect you're either missing RELATIVE:
FILE(GLOB_RECURSE ALL_Lib_CPP_SRCS RELATIVE "src/Library/" "*.cpp")
Or you have an extra space in the globbing expression:
FILE(GLOB_RECURSE ALL_Lib_CPP_SRCS "src/Library/*.cpp")
I suspect the second option above is what you want. The quotes are optional, but I would recommend their use, as it makes the line easier to read IMO.
The second issue is that you need a ${} when you reference the variable:
add_library(MyLibrary STATIC ${ALL_Lib_CPP_SRCS})
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)
Task -> Run Cmake to generate dll in windows and shared object(.so files) in linux.
I am porting code from windows to linux . I have made the required changes in source code. I have tried changing the CMakeLists.txt files but when I tried to do make it complains of "Cannot find source files". May you guys please point me out where I am making mistake. CMakeList.txt is shown below.
The cmake runs fine in windows but in linux it complains about the error.
I have commented few lines and added line in CMakeLIsts.txt to make it work in Linux but it doesn't work.
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
CMAKE_POLICY( SET CMP0017 NEW )
PROJECT( disk_space_model )
INCLUDE( ../libs/helper_functions.cmake )
INCLUDE_THIRD_PARTY_SFC()
SET( HEADER_FILES
stdafx.h
INS_sensor_model.h
)
SET( SOURCE_FILES
Disk_space_model.cpp
)
SET( RESOURCE_FILES
"Disk Space DLL.rc"
resource.h
)
COMMON_SETUP()
SETUP_Lab_LIB_FILES( lib_foo lib_boo_files lib_platform lib_utils )
#ADD_DEFINITIONS(-D_USRDLL -DINSSENSORDLL_EXPORTS)
#ADD_LIBRARY( disk_space_model SHARED ${SOURCE_FILES} ${HEADER_FILES} ${RESOURCE_FILES} ${CMAKE_HELPER_FILES} )
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--export-all-symbols")
ADD_LIBRARY( disk_space_model [ STATIC | SHARED ] ${SOURCE_FILES} ${HEADER_FILES} ${RESOURCE_FILES} ${CMAKE_HELPER_FILES} )
SET_OUTPUT_DIRS( disk_space_model )
TARGET_LINK_LIBRARIES( disk_space_model ${LIB_FILES} )
Below are the following errors that I have received when I do make after congiure and generate on CMake-gui
4 bit compiler
-- Configuring done
CMake Error at CMakeLists.txt:37 (ADD_LIBRARY):
Cannot find source file:
[
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error in CMakeLists.txt:
Cannot find source file:
STATIC
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error in CMakeLists.txt:
Cannot find source file:
|
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error in CMakeLists.txt:
Cannot find source file:
SHARED
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error in CMakeLists.txt:
Cannot find source file:
]
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/mydir/svn/proj1/apps/disk space
make: *** [cmake_check_build_system] Error 1
It's broken because of this:
ADD_LIBRARY( disk_space_model [ STATIC | SHARED ] ${SOURCE_FILES} ${HEADER_FILES} ${RESOURCE_FILES} ${CMAKE_HELPER_FILES} )
I guess you copy-pasted [ STATIC | SHARED ] from the documentation. That's supposed to be a choice of either STATIC or SHARED (the latter, in your case).