I am going back-and-forth with this confusing concept of CMake. I have read a few articles describing how to write CMake file(s). But I found very less explaination for it.
Point 1 - Do I have to make separate CMakeLists.txt for each folder (Yes, let's call it a folder for simplicity)?
Point 2 - Apparently Answer to Point 1 is NO. I found these commands to automate the search for *.cpp
files
# get all *.cpp files recursively
file(GLOB_RECURSE SRC_LIST *.c* *.h*)
Point 3 - There seems no need of mentioning *.hpp
file. (This is vague point)
I have tried compiling THE SIMPLEST Hello World! program
CMakeLists.txt
src
| - main.cpp (here I included #include "XYZ/abc.hpp")
| - XYZ
| - abc.cpp (included #include "XYZ/abc.hpp")
| - abc.hpp
And the CMake for this was
cmake_minimum_required(VERSION 3.10)
project(CMAkeSample)
add_executable(hello src/main.cpp src/XYZ/abc.cpp)
That suggest, add all .cpp
files along with path in the add_executable
and we are done.
Is this true?
Point 4 - Now I have a comparitively bigger project (developed in eclipse for whatever reason). But I need to make cmake file for it and thus not use the eclipse for building.
below is the file system and I want to make CMakeLists.txt
just once (if possible)
CMakeLists.txt
src
| - main.cpp (here I included #include "File0.hpp" and #include "Folder1/File1.hpp")
| - File0.cpp (here I included #include "File0.hpp")
| - File0.hpp (here I included #include "Folder1/File1.hpp" and #include "Folder2/File2.hpp")
| - Folder1
| - File1.cpp (included #include "File1.hpp")
| - File1.hpp
| - Folder2
| - File2.cpp (included #include "File2.hpp")
| - File2.hpp
So now How to get all the .cpp
files without manually writing cmake in each folder. Any suggestions?
UPDATE 1:
I have added the CMakeLists.txt
like shown
ProjectFolder
CMakeLists.txt
src
| - CMakeLists.txt
| - main.cpp (here I included #include "File0.hpp" and #include "Folder1/File1.hpp")
| - File0.cpp (here I included #include "File0.hpp")
| - File0.hpp (here I included #include "Folder1/File1.hpp" and #include "Folder2/File2.hpp")
| - Folder1
| - CMakeLists.txt
| - File1.cpp (included #include "File1.hpp")
| - File1.hpp
| - Folder2
| - CMakeLists.txt
| - File2.cpp (included #include "File2.hpp")
| - File2.hpp
In ProjectFolder > src > CMakeLists.txt
add_library(main main.cpp File0.cpp File0.hpp)
target_link_libraries(main File1)
In ProjectFolder > src > File1 > CMakeLists.txt
add_library(File1 File1.cpp File1.hpp)
target_link_libraries(File1 File2)
In ProjectFolder > src > File > CMakeLists.txt
add_library(File2 File2.cpp File2.hpp)
After this all, I am getting the error as,
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/ProjectFolder
[ 33%] Built target main
[ 44%] Building CXX object CMakeFiles/ProjectFolder.dir/src/main.cpp.o
[ 55%] Linking CXX executable ProjectFolder
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: cannot find -lFile1
UPDATE 2:
Moving on, To make this work, Few changes here and there were made (as summarised in UPDATE 1 and added linking manually in the Project Folder's CMakeLists.txt as
set(GCC_LINK_FLAGS "-lpthread -lcurl -lgobject-2.0 -lgstreamer-1.0 -lglib-2.0 -lssl -lcrypto -lboost_thread -lboost_system")
add_subdirectory(src)
add_subdirectory(src/Folder1)
add_subdirectory(src/Folder2)
add_executable(ProjectFolder src/main.cpp)
target_link_libraries(ProjectFolder main ${GCC_LINK_FLAGS})
Program executes perfectly. But obviously this is very crude way of writing the linkings. Looking for the solution to Point2.