Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CMake to build libraries and project #23

Merged
merged 13 commits into from
Jul 25, 2024
Merged

Conversation

jeannielynnmoulton
Copy link
Collaborator

@jeannielynnmoulton jeannielynnmoulton commented Jul 18, 2024

This changes to using CMake to build. Now instead of compiling with g++ directly, we can use CMake commands. This is better because we don't have to worry about OS dependent commands.

This also moves tinyxml out of the src folder and into lib/tinyxml and links it as a library.

Copy link

@tlestang tlestang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's already taking us pretty far! I noted a few things mostly related to writing modern CMake

file(GLOB sources *.cpp)
add_executable(transfil_N ${sources})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments here:

  • Is -std=c++17 required since CMAKE_CXX_STANDARD is already specified in the top-level CMakeLists.txt?
  • The debug flag doesn't have to be hardcoded as a compiler option here. Debug mode can be set at cmake invocation using -DCMAKE_BUILD_TYPE (docs)
  • The variable CMAKE_CXX_FLAGS sets flags globally for all targets. Not a problem in this particular case, but if the project grows, we might be better off setting flags using target_compile_options instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This set(CMAKE_CXX_FLAGS ...) is now totally removed.


find_package(GSL REQUIRED)
# Include the library directories for linking
target_include_directories(transfil_N PUBLIC

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use PRIVATE for clarity. As far as I know, PUBLIC doesn't mean much for an executable target.

-lstdc++fs \
-Wall -O3 -std=c++17
cmake .
cmake --build .

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very satisfying 🧹

$( $(brew --prefix)/bin/gsl-config --libs ) \
$( $(brew --prefix)/bin/gsl-config --cflags ) \
-Wall -O3 -std=c++17
cmake --build .

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's usually good practice to use out-of-source builds:

mkdir build && cd build/
cmake --build ..

find_package(GSL REQUIRED)
# Include the library directories for linking
target_include_directories(transfil_N PUBLIC
${PROJECT_BINARY_DIR}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would encourage you to use an out-of-source build layout, and specify location of private headers using CMAKE_CURRENT_SOURCE_DIR. Using PROJECT_BINARY_DIR like this would break as soon as an other project call is used below the top level one.

# Include the library directories for linking
target_include_directories(transfil_N PUBLIC
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/lib/tinyxml

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The keyword PUBLIC was used in the call the target_link_directories for the tinyxml target. Therefore its LINK_DIRECTORIES property is inhereted by targets that link to it and lib/tinyxml doesn't need to be re-specified here.

@@ -0,0 +1,12 @@
file(GLOB sources *.cpp)
Copy link

@tlestang tlestang Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting sources using glob patterns is usually discouraged in favor of specifying file names explicitly.

The reason is that, using the glob pattern, the generated build system (e.g. make) won't be able to trigger a (re)generation if files are added or removed, for instance when an older revision of the project is checked out.

See the note at https://cmake.org/cmake/help/latest/command/file.html#filesystem

Base automatically changed from jeannie/useConsistentSeed to main July 25, 2024 08:53
Using GLOB is against recommend CMake best practices.
@jeannielynnmoulton jeannielynnmoulton merged commit 345fc11 into main Jul 25, 2024
6 checks passed
@jeannielynnmoulton jeannielynnmoulton deleted the jeannie/usecmake branch July 25, 2024 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants