Adds CMake build support#155
Conversation
|
Ambed has a single makefile, no variants, no shared code, sits in a single folder and compiles flawless doing a simple make. Using CMake adds an additional step and an additional tool to install, which in the end comes down to the same make to be used. |
|
I agree with you that maintaining two different build systems could be a pain and requires more effort to keep them aligned, but using CMake could be very useful if you want to have a standard build, an automated install system, or if you want to package the software as a DEB or an RPM. I also noticed that all the configuration params are hardcoded on main.h, especially for xlxd. Modifying them (to have working files in a specific directory, or to let daemons easily run on non-root users) it's a problem if you want to keep the source code aligned with git repository. Some params could be moved on a dedicated header file produced by cmake configuration (even better in a dedicated config file read in runtime). About CLion, it requires CMake on project for importing, analyzing source code and other stuffs, but it's just my favorite IDE for C/C++, absolutely not a requirement for the project. |
|
Sorry, I wrongly pushed two more commits on my master branch. |
It is good practice to do work in progress on a different branch and to pull only finished stuff into master. ;) |
|
The current Makefile had the beauty to automatically include new source files in the build. It looks to me that now we would have to update CMakeLists.txt. Isn't there a way to automate this? |
|
... and technically CMake works on top of Makefile... ))) |
Yes. It's possible to set a variable with GLOB and include all *.cpp and *.h files, and it works, but it's considered a bad way to use cmake, because generated makefiles still contains an explicit list of source files to build. It's also discouraged in the official documentation:
I still prefer explicit file list in CMakeLists, fut for a small project like this, I think that It's can be considered a solution. |
Thank you for taking time to explain, I am a noob concerning CMake... |
Adding a new file in CMake consists on just adding the filename in related CMakeLists.txt, no more steps. This is an example: Original add_executable(myapp
config.hpp
main.cpp main.hpp
utilities.cpp utilities.hpp)
target_link_libraries(myapp
LINK_PUBLIC
Threads::Threads)
target_compile_options(myapp
PRIVATE
-Wall)You want to add a class named
add_executable(myapp
config.hpp
main.cpp main.hpp
utilities.cpp utilities.hpp
socket_tool.cpp socket_tool.hpp)
target_link_libraries(myapp
LINK_PUBLIC
Threads::Threads)
target_compile_options(myapp
PRIVATE
-Wall)File order is not important, I just prefer to put them in different lines, one for each couple of Files can be also added if using a variable (discouraged way to use cmake): set(MYAPP_SOURCES
main.cpp
utilities.cpp
socket_tool.cpp)
set(MYAPP_HEADERS
config.hpp
main.hpp
utilities.hpp
socket_tool.hpp)
add_executable(myapp
${MYAPP_SOURCES}
${MYAPP_HEADERS})
target_link_libraries(myapp
LINK_PUBLIC
Threads::Threads)
target_compile_options(myapp
PRIVATE
-Wall)As last example, GLOBs can be used to avoid modifying CMake each time a new class is created: file(GLOB MYAPP_SOURCES *.cpp)
file(GLOB MYAPP_HEADERS *.hpp)
add_executable(myapp
${MYAPP_SOURCES}
${MYAPP_HEADERS})
target_link_libraries(myapp
LINK_PUBLIC
Threads::Threads)
target_compile_options(myapp
PRIVATE
-Wall)Last one is not recommended: https://cmake.org/cmake/help/v3.17/command/file.html?highlight=glob#filesystem. |
Same way possible in Makefile :) You just need to be able to cook it )) |
This commit will add CMake build system support, useful for building and editing project in JetBrains CLion.
It also adds required entries in gitignore.