Coder Social home page Coder Social logo

cmake-target's Introduction

Demonstration of using CMake interface targets as metadata collections, rather than having to learn all the variables associated with a target.

The Old

Early CMake libraries would define a whole slew of variables that you had to be aware of an integrate into your own cmake files:

### OLD: DO NOT USE
find_package (Python3)
...

add_executable(MyExe main.cpp)
# Make sure WE set the right compiler flags...
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PYTHON_COMPILER_FLAGS}")
# Make sure WE tell the compiler where python's includes are...
include_directories(${PYTHON_INCLUDE_DIRS})
...

But modern CMake emphasizes associating variables (aka properties) and all that compile-time information with your target at the definition point, so that consumers can simply bring it all in when they link to your target.

The point of confusion for everyone is that this makes the terms "library" and "link" overloaded.

Worse, if you are new to CMake, you're expected to discover this thru reading about the 'VISIBILITY' properties of libraries, which I'm sure was the first thing you looked up in the manpages, right?

# # Old #1: Set a global variable with my include directories, consumers need
# # to reference it.

#provider
add_library(somelib ...)
set (SOMELIBS_LIBS         somelib pthread ...)
set (SOMELIBS_CFLAGS       $SOMELIBS_CFLAGS -DUSE_SOMELIB=1 -Werror)   # because that won't hurt will it?
set (SOMELIBS_INCLUDE_DIRS ${SOMELIBS_INCLUDE_DIRS} /somewhere/over/the/rainbow)

#consumer
set (CMAKE_CFLAGS ${CMAKE_CFLAGS} ${SOMELIBS_CFLAGS} ...)
include_directories (... ${SOMELIBS_INCLUDE_DIRS}

add_executable (my_prog main.cpp)

target_link_libraries (my_prog ${SOMELIB_LIBS})
# # Old #2: Just force everything to build the way your library wants

# provider
add_library (somelib ...)
include_directories (/somewhere/over/the/rainbow)
set (CMAKE_CFLAGS ${CMAKE_CFLAGS} -DUSE_SOMELIB=1 -Werror)  # world of hurt
set (SOMELIBS_LIBS somelib pthread ...)  # still have to use variables too

#consumer
add_executable (my_prog main.cpp)
target_link_libraries (my_prog ${SOMELIB_LIBS})
# # Modern

# provider
add_library (somelib ...)
target_link_libraries (somelib PUBLIC pthread ...)
target_compile_options (somelib PRIVATE -Werror)  # you don't have to -Werror with me
target_compile_definitions (somelib PUBLIC -DUSE_SOMELIB=1)  # you'll need this to use me
target_include_directories (my_compile_flags /somewhere/over/the/rainbow)


# consume
add_executable (my_prog main.cpp)
target_link_libraries (my_prog somelib)

Doing the experiment

My standard recommended build method is:

> cmake -G Ninja -DCMAKE_VERBOSE_MAKEFILE=ON -S . -B build
> cmake --build ./build

(* assumes powershell or unix shell)

Compile and build. The program doesn't do anything, but it won't build & link if it didn't have access to Python correctly.

cmake-target's People

Contributors

kfsone avatar

Watchers

 avatar

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.