CMake Error at CMakeLists.txt:4 (add_executable) - c++

I am trying to build a simple CNN model using Pytorch C++ API ( libtorch ), but when I try to run CMake it gives me this error:
CMake Error at CMakeLists.txt:4 (add_executable):
Cannot find source file:
training_VGG.cpp
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: example
CMake Error: Cannot determine link language for target "example".
----------------------------------------------------------------------------
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(Digit-Recognition-MNIST)
find_package(Torch REQUIRED)
add_executable(example training_VGG.cpp)
target_link_libraries(example "${TORCH_LIBRARIES}")
set_property(TARGET example PROPERTY CXX_STANDARD 11)
I hope anyone can help me.

Related

How to add library to C++ Project using homebrew, cMake and, cLion

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.

CMake cannot find source file

I am trying to configure PCL With visual studio but I am facing an error when using cmake. I am using pcl 1.8 with VS 2013 and cmake 3.14. I could successfully did the configuration step but when I move to generate, I do face add_executable error.
this is my cmakelists
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(cloud_viewer)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (cloud_viewer cloud_viewer.cpp)
target_link_libraries (cloud_viewer ${PCL_LIBRARIES})
This is the error I get:
CMake Error at CMakeLists.txt:11 (add_executable): Cannot find source
file: cloud_viewer.cpp 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:11 (add_executable): No SOURCES given to target:
cloud_viewer
Image that shows both CMakelists.txt and cloud_viewer being in the same folder
Could anyone help me solving it. thanks in advance.
Looking at your screenshot, you should name your file cloud_viewer.cpp, not cloud_viewer.ccp. Be careful about the extension: cpp vs ccp
On a side note, try using the cmake taget based system instead of the directory based:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(cloud_viewer)
find_package(PCL 1.2 REQUIRED)
add_executable(cloud_viewer cloud_viewer.cpp)
target_link_libraries(cloud_viewer PUBLIC ${PCL_LIBRARIES})
target_compile_definitions(cloud_viewer PUBLIC ${PCL_DEFINITIONS})
target_include_directories(cloud_viewer PUBLIC ${PCL_INCLUDE_DIRS})

cmake: "add_custom_target" + PRE_BUILD didn't take effect while building

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.

CMake problems with CLion - Error:Cannot determine link language for target "XYZ"

Starting up CLion outputs me the following:
Error:Cannot find source file:
sinclude/MathCompiler.h
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx
Error:CMake can not determine linker language for target: MathCompiler
Error:Cannot determine link language for target "MathCompiler".
Warning:Configuration Debug
Unable to determine product file path for target MathCompiler (Debug). Running and debugging will be unavailable.
Warning:Configuration Release
Unable to determine product file path for target MathCompiler (Release). Running and debugging will be unavailable.
Warning:Configuration RelWithDebInfo
Unable to determine product file path for target MathCompiler (RelWithDebInfo). Running and debugging will be unavailable.
Warning:Configuration MinSizeRel
Unable to determine product file path for target MathCompiler (MinSizeRel). Running and debugging will be unavailable.
I didn't change anything since the last build.
Here is CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
project(MathCompiler)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp cpp/MathCompiler.cpp include/MathCompiler.h cpp/MathException.cpp include/MathException.h cpp/MathematicString.cpp include/MathematicString.h include/str_cmp.h include/typedefs.h)
add_executable(MathCompiler ${SOURCE_FILES})
.h-files usually aren't included into the list of source files

glob_recurse generate error in cmake

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})