ljh
(ljh)
February 27, 2023, 6:31pm
1
Hi community,
I wonder why CMake does not support these four settings directly:
CPPFLAGS
, CFLAGS
, LDFLAGS
, LDLIBS
.
I list their correspondings in CMake. I’m not sure if they are correct.
CPPFLAGS: include_directories(./foo), add_compile_definitions(VAR)
CFLAGS : add_compile_options(-std=c11)
LDFLAGS : add_link_options(-L./foo)
LDLIBS : link_libraries(foo)
The other day, I tried to use both -L
and -l
with
add_link_options(-L./foo -lfoo)
It fails to compile becuase -l
belongs to LDLIBS and should occurs
after .o
object files.
I think Autotools does not have LDLIBS but I still can put both -L
and -l
with
LDFLAGS=-L./foo -lfoo
Autotools uses libtool to arrange -l
after .o
object files, and it compiles.
You should use [target_]link_directories
for -L
flags and [target_]link_libraries
for -l
flags. Better is to construct the absolute path and just use that directly.
ljh
(ljh)
March 3, 2023, 8:19am
3
Ben Boeckel:
link_directories
Hi Ben,
All the compiling and linking options can be set by
these six CMake commands, right?
link_directories # LDFLAGS += -L../foo
link_libraries # LDLIBS += -lfoo
include_directories # CPPFLAGS += -I../foo
add_compile_definitions # CPPFLAGS += -DNDEBUG
add_link_options(-fsanitize=address) # LDFLAGS += ..
add_compile_options(-std=c11) # CFLAGS += ..
Can I have some suggestions on my little CMakeLists.txt as follow?
Thanks
$ vi CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(MyProject VERSION 1.0)
set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
set(CMAKE_INSTALL_RPATH $ORIGIN/../lib)
if (CMAKE_BUILD_TYPE STREQUAL Debug)
add_link_options(-fsanitize=address)
endif()
#link_libraries(foo)
#link_directories(../foo)
#include_directories(../foo)
#add_compile_definitions(VAR) # VAR=value
add_compile_options(-std=c11) # .cpp
#set(CMAKE_C_COMPILER gcc) # .cpp
file(GLOB SOURCES *.c) # .cpp
add_executable(main ${SOURCES})
#add_library(foo SHARED ${SOURCES})
#set_target_properties(foo PROPERTIES VERSION 1.2.3 SOVERSION 1)
#add_subdirectory(./foo)
# make -C build DESTDIR=~/foo install
install(TARGETS main) # foo)
#file(GLOB LIBRARIES ../lib/*.so*)
#install(FILES ${LIBRARIES} DESTINATION lib)
# cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug # Release
# cmake --build build -v # verbose
$