Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

build - FindPkgConfig with set_target_properties is unusable for setting CFLAGS/LDFLAGS

pkg_check_modules from FindPkgConfig gives MYLIBRARY_LDFLAGS and MYLIBRARY_CFLAGS that are ordinary CMake lists (with semicolon-separator).

The set_target_properties and set_property accept just one string.

So that doesn't work because it doesn't expand the list:

set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY LINK_FLAGS ${MYLIBRARY_LDFLAGS})

This gives same thing with the semicolons inside:

set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY LINK_FLAGS "${MYLIBRARY_LDFLAGS}")

set_target_properties expands into multiple strings when unquoted and into one string with semicolons when quoted.

How am I supposed to use that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Common workflow with libraries searched by pkg-config within CMake:

# Use pkg-config for search library `xxx`.
pkg_check_modules(XXX xxx)

# Build target which uses given library

# The only command which has no target-oriented equivalent.
# But see comments after the code.
link_directories(${XXX_LIBRARY_DIRS}) 

# Two global commands belows can be replaced by target-oriented equivalent
# after creation of the target.
include_directories(${XXX_INCLUDE_DIRS})
add_compile_options(${XXX_CFLAGS_OTHER})

# Create target
add_executable(my_exe ...) # Or add_library()

# The only target-oriented command, which has no global equivalent.
target_link_libraries(my_exe ${XXX_LDFLAGS_OTHER}) # It is OK to have link flags here
target_link_libraries(my_exe ${XXX_LIBRARIES}) # Can be combined with previous call.

Note, that we use XXX_LDFLAGS_OTHER variable instead of XXX_LDFLAGS one. This is because XXX_LDFLAGS includes -l and -L options for which CMake has more suitable commands. Similar reason about usage of XXX_CFLAGS_OTHER.


Currently, CMake doesn't recommend to use link_directories command but use absolute paths to libraries in target_link_libraries call. One can extract absolute paths to libraries, listed by pkg-config, with use of find_library command:

...
# Instead of using `link_directories`, collect absolute paths to libraries.
set(XXX_LIBS_ABSOLUTE)
foreach(lib ${XXX_LIBRARIES})
    # Choose name of variable, which will contain result of `find_library`
    # for specific library.
    set(var_name XXX_${lib}_ABS)
    # Search library under dirs, returned by pkg-config.
    find_library(${var_name} ${lib} ${XXX_LIBRARY_DIR})
    list(APPEND XXX_LIBS_ABSOLUTE ${${var_name}})
endforeach()

# Instead of `target_link_libraries(my_exe ${XXX_LIBRARIES})`
target_link_libraries(my_exe ${XXX_LIBS_ABSOLUTE})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...