# About LDLIBS etc.

**URL:** https://discourse.cmake.org/t/about-ldlibs-etc/7558
**Category:** Usage
**Created:** [February 27, 2023, 6:31pm UTC](https://discourse.cmake.org/t/about-ldlibs-etc/7558 "2023-02-27T18:31:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ljh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@ljh](https://discourse.cmake.org/u/ljh)
#### Post date: [February 27, 2023, 6:31pm UTC](https://discourse.cmake.org/t/about-ldlibs-etc/7558/1 "2023-02-27T18:31:55Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 27, 2023, 8:09pm UTC](https://discourse.cmake.org/t/about-ldlibs-etc/7558/2 "2023-02-27T20:09:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ljh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@ljh](https://discourse.cmake.org/u/ljh)
#### Post date: [March 3, 2023, 8:19am UTC](https://discourse.cmake.org/t/about-ldlibs-etc/7558/3 "2023-03-03T08:19:52Z")

</div>

> [@ben.boeckel](#):
>
> link\_directories

Hi Ben,

All the compiling and linking options can be set by  
these six CMake commands, right?

```auto
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

```auto
$ 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

$

```
