Git Product home page Git Product logo

Comments (8)

Leon0402 avatar Leon0402 commented on July 30, 2024 1

Well the target_link_libraries is actually not (only) about linking C++ libraries. So you can link C++ libraries like mylib.a or something like this, but it is highly discouraged.
Instead, CMake works with targets, which is a form of abstraction. Cmake targets can be anything like classic C++ Libraries, Exectuables, Header Only libraries, .... Think of it like metadata. It contains information about source files (cpp & h), compiler flags, compiler definitions, include paths, dependencies, ...
In modern Cmake all of these settings are associated to a target as either PRIVATE, INTERFACE or PUBLIC. Private means the setting is just for the target itself (for instance the cpp files), INTERFACE means the setting is just for targets dependencing on this target (For instance the include path in a header only library), PUBLIC applies for the target itself and the dependent targets (for instance the include path is a non header only library). So the important thing is that targets can with PUBLIC / INTERFACE kind of inherit information to other targets.

So target_link_libraries should not be used to link a raw C++ library to a cmake target, but actually to link a cmake target to another target. This leads to this "interhitance" of the properties.

As an example take the trompeloeil::trompeloeil target. It it header only as you said, but still there is a cmake target defined. This target is of type "INTERFACE Library". So it specified option which will only be inherited to other options. For instance the target might specify an include path as INTERFACE.
If you do target_link_libraries(test PUBLIC trompeloeil::trompeloeil) the interface include path of the trompeloeil target is inherited to the test target.

The real power of all this is when it comed to dependencies of dependencies. For instance trompeloeil could link against the boost target (in case it would need that) and then you test target would also receive all information about boost. No need to specify anything yourself. In theory you should never need much more than just the line to find the target (find_package or FetchContent) and the linking against the target. Unfortunatly many libraries don't use modern cmake, so at the moment you probably will experience that it is not as easy as that. Best thing you can do in such a case is to report it to maintainers or even better fix it.

I can only recommend you to learn more about targets and how they work. It's really the most crucial concept of cmake and it will help you a lot to master it.

from trompeloeil.

Leon0402 avatar Leon0402 commented on July 30, 2024

Haven't actually tried it out with a more recent version, but used to work without any problems (v39), so it might be a regression.

You are right that it should work out of the box. But in case of a regression, you can at least use:

target_include_directories(<my target name> PRIVATE "${trompeloeil_SOURCE_DIR}/include")

to make it a little bit less ugly :)

from trompeloeil.

rollbear avatar rollbear commented on July 30, 2024

Can you explain what you are seeing?

I've tried this as a dummy project, and it works fine.

CMakeLists.txt

cmake_minimum_required(VERSION 3.11)
project(tst)
include(FetchContent)
FetchContent_Declare(
  trompeloeil
  GIT_REPOSITORY https://github.com/rollbear/trompeloeil.git
  GIT_TAG v43)

FetchContent_MakeAvailable(trompeloeil)

add_executable(
  test
  test.cpp
)

test.cpp

#include <trompeloeil.hpp>

int main()
{
}

from trompeloeil.

rollbear avatar rollbear commented on July 30, 2024

OK, never mind, I can reproduce now. Looking into it.

from trompeloeil.

rollbear avatar rollbear commented on July 30, 2024

No, actually, I can't reproduce. If I add:

target_link_libraries(
  test
  PUBLIC
  trompeloeil::trompeloeil
)

to CMakeLists.txt above, it works.

I have the sources in /tmp/1, and run cmake and make. I add -DCMAKE_VERBOSE_MAKEFILE=yes to get visibility into the process:

bjorn@protopteryx /t/2> cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_VERBOSE_MAKEFILE=yes /tmp/1
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/2
bjorn@protopteryx /t/2> make
/usr/bin/cmake -S/tmp/1 -B/tmp/2 --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /tmp/2/CMakeFiles /tmp/2//CMakeFiles/progress.marks
make  -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/tmp/2'
make  -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/depend
make[2]: Entering directory '/tmp/2'
cd /tmp/2 && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /tmp/1 /tmp/1 /tmp/2 /tmp/2 /tmp/2/CMakeFiles/test.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/tmp/2'
make  -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
make[2]: Entering directory '/tmp/2'
[ 50%] Building CXX object CMakeFiles/test.dir/test.cpp.o
/usr/bin/c++  -I/tmp/2/_deps/trompeloeil-src/include -g -MD -MT CMakeFiles/test.dir/test.cpp.o -MF CMakeFiles/test.dir/test.cpp.o.d -o CMakeFiles/test.dir/test.cpp.o -c /tmp/1/test.cpp
[100%] Linking CXX executable test
/usr/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/usr/bin/c++ -g CMakeFiles/test.dir/test.cpp.o -o test 
make[2]: Leaving directory '/tmp/2'
[100%] Built target test
make[1]: Leaving directory '/tmp/2'
/usr/bin/cmake -E cmake_progress_start /tmp/2/CMakeFiles 0

It builds it fine with the right include path.

What are the steps you do, and what are the observations?

from trompeloeil.

kevinchannon avatar kevinchannon commented on July 30, 2024

Ah, I see. I didn't have anything in the target_link_libraries call. Trompeloeil is header-only library, so I had assumed the linker didn't need to care about it. Seems strange that FetchContent doesn't add it to the include paths unless you tell the linker about it, right? Maybe I don't understand CMake/compilers/linkers well enough!

EDIT: Just to confirm, if I add the link libraries bit, things work fine.

from trompeloeil.

kevinchannon avatar kevinchannon commented on July 30, 2024

Thanks! An absolutely brilliant explanation.

from trompeloeil.

rollbear avatar rollbear commented on July 30, 2024

Awesome @Leon0402 . Thank you!

from trompeloeil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.