Quantcast
Viewing all articles
Browse latest Browse all 4824

C/C++ • Re: Need help to build a working make file

IMHO, manually writing and maintaining Makefiles is a waste of time.
These days, CMake is the de facto standard for building C++ code. Even though the syntax takes some time to get used to, it's not nearly as cryptic as Make.

For example:

Code:

# CMakeLists.txt (at the root of your project)cmake_minimum_required(VERSION 3.18)project(RudolfAtHome LANGUAGES CXX)find_package(Threads REQUIRED)add_library(warnings INTERFACE)target_compile_options(warnings INTERFACE -Wall -Wextra)add_library(a "libs/lib_a.cpp")target_include_directories(a PUBLIC "libs")target_link_libraries(a PRIVATE warnings PUBLIC bcm2835 rt)add_library(b "libs/lib_b.cpp")target_include_directories(b PUBLIC "libs")target_link_libraries(b PRIVATE warnings PUBLIC Threads::Threads)add_library(c "libs/lib_c.cpp")target_include_directories(c PUBLIC "libs")target_link_libraries(c PRIVATE warnings)add_executable(application_a "src/application_a.cpp")target_link_libraries(application_a PRIVATE warnings a b c)
Then compile it using:

Code:

cmake -S . -B buildcmake --build build
The basics are very simple:
  1. Define "targets" (libraries and executables) using add_library and add_executable;
  2. Set the properties of those targets (e.g. the include path);
  3. Use target_link_libraries to express dependencies between targets.
Here library "a" depends on the bcm2835 and rt libraries, and "b" depends on the threading library. The application depends on all libraries. All targets with your own code should depend privately on the warnings target, so that CMake automatically propagates the warnings flags when compiling your own code, but not to a user's code that depends on your libraries.

Statistics: Posted by tttapa — Sun Mar 31, 2024 9:00 pm



Viewing all articles
Browse latest Browse all 4824

Trending Articles