This is kind of a hack, but for a C++ project, you can use CMAKE_CXX_STANDARD_LIBRARIES. For a C project, I think you would use CMAKE_C_STANDARD_LIRBARIES.

Example for C++ that links to libbar and libfoo:

cmake ... -DCMAKE_CXX_STANDARD_LIBRARIES="-lbar -lfoo"

See the documentation here:

https://cmake.org/cmake/help/v3.6/variable/CMAKE_LANG_STANDARD_LIBRARIES.html

This won't be available for older versions of CMake; it was added some time after version 3.0.

Answer from David Grayson on Stack Overflow
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ latest โ€บ command โ€บ target_link_libraries.html
target_link_libraries โ€” CMake 4.3.0-rc3 Documentation
A plain library name: The generated link line will ask the linker to search for the library (e.g. foo becomes -lfoo or foo.lib). The library name/flag is treated as a command-line string fragment and will be used with no extra quoting or escaping.
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ v3.0 โ€บ command โ€บ target_link_libraries.html
target_link_libraries โ€” CMake 3.0.2 Documentation
target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> <lib> ... [<PRIVATE|PUBLIC|INTERFACE> <lib> ... ] ...]) The PUBLIC, PRIVATE and INTERFACE keywords can be used to specify both the link dependencies and the link interface in one command. Libraries and targets following PUBLIC are linked to, and are made part of the link interface.
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ latest โ€บ command โ€บ link_directories.html
link_directories โ€” CMake 4.3.0-rc3 Documentation
The find_library() command provides the full path, which can generally be used directly in calls to target_link_libraries().
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ latest โ€บ variable โ€บ CMAKE_LINK_LIBRARY_USING_FEATURE.html
CMAKE_LINK_LIBRARY_USING_ โ€” CMake 4.3.0-rc3 Documentation
if (CMAKE_C_COMPILER_ID STREQUAL "AppleClang") set(CMAKE_LINK_LIBRARY_USING_weak_library "PATH{-weak_library <LIBRARY>}NAME{LINKER:-weak-l<LIB_ITEM>}" ) set(CMAKE_LINK_LIBRARY_USING_weak_library_SUPPORTED TRUE) endif() add_library(lib SHARED ...) add_executable(main ...) if(CMAKE_LINK_LIBRARY_USING_weak_library_SUPPORTED) target_link_libraries(main PRIVATE "$<LINK_LIBRARY:weak_library,lib,external>") else() target_link_libraries(main PRIVATE lib external) endif() CMake will generate the following linker command line fragment when linking main using the AppleClang toolchain:
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ latest โ€บ command โ€บ add_library.html
add_library โ€” CMake 4.3.0-rc3 Documentation
Added in version 3.12: Object libraries can be linked to with target_link_libraries(). ... Add an Interface Library target that may specify usage requirements for dependents but does not compile sources and does not produce a library artifact on disk. An interface library with no source files is not included as a target in the generated buildsystem.
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ v3.7 โ€บ command โ€บ target_link_libraries.html
target_link_libraries โ€” CMake 3.7.2 Documentation
The named <target> must have been created in the current directory by a command such as add_executable() or add_library(). Repeated calls for the same <target> append items in the order called. Each <item> may be: A library target name: The generated link line will have the full path to the linkable library file associated with the target.
Find elsewhere
๐ŸŒ
Gitlab
cliutils.gitlab.io โ€บ modern-cmake โ€บ chapters โ€บ basics.html
Introduction to the basics โ€” Modern CMake
If no target of that name (one) exists, then it adds a link to a library called one on your path (hence the name of the command). Or you can give it a full path to a library. Or a linker flag. Just to add a final bit of confusion, classic CMake allowed you to skip the keyword selection of PUBLIC, etc.
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ latest โ€บ command โ€บ add_link_options.html
add_link_options โ€” CMake 4.3.0-rc3 Documentation
This command can be used to add any link options, but alternative commands exist to add libraries (target_link_libraries() or link_libraries()).
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ v3.9 โ€บ command โ€บ target_link_libraries.html
target_link_libraries โ€” CMake 3.9.6 Documentation
The named <target> must have been created in the current directory by a command such as add_executable() or add_library(). Repeated calls for the same <target> append items in the order called. Each <item> may be: A library target name: The generated link line will have the full path to the linkable library file associated with the target.
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ v3.2 โ€บ command โ€บ target_link_libraries.html
target_link_libraries โ€” CMake 3.2.3 Documentation
target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> <lib> ... [<PRIVATE|PUBLIC|INTERFACE> <lib> ... ] ...]) The PUBLIC, PRIVATE and INTERFACE keywords can be used to specify both the link dependencies and the link interface in one command. Libraries and targets following PUBLIC are linked to, and are made part of the link interface.
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ latest โ€บ command โ€บ target_link_options.html
target_link_options โ€” CMake 4.3.0-rc3 Documentation
Add options to the link step for an executable, shared library or module library target. target_link_options(<target> [BEFORE] {INTERFACE|PUBLIC|PRIVATE} <item>... [{INTERFACE|PUBLIC|PRIVATE} <item>...]...) The named <target> must have been created by a command such as add_executable() or add_library() and must not be an ALIAS target.
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ v3.6 โ€บ command โ€บ target_link_libraries.html
target_link_libraries โ€” CMake 3.6.3 Documentation
The named <target> must have been created in the current directory by a command such as add_executable() or add_library(). Repeated calls for the same <target> append items in the order called. Each <item> may be: A library target name: The generated link line will have the full path to the linkable library file associated with the target.
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ v3.13 โ€บ command โ€บ link_directories.html
link_directories โ€” CMake 3.13.5 Documentation
The find_library() command provides the full path, which can generally be used directly in calls to target_link_libraries().
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ latest โ€บ command โ€บ target_link_directories.html
target_link_directories โ€” CMake 4.2.3 Documentation
The find_library() command provides the full path, which can generally be used directly in calls to target_link_libraries().
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ v3.13 โ€บ command โ€บ add_link_options.html
add_link_options โ€” CMake 3.13.5 Documentation
See documentation of the directory and target LINK_OPTIONS properties. This command can be used to add any options, but alternative commands exist to add libraries (target_link_libraries() or link_libraries()).
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ v3.0 โ€บ command โ€บ link_directories.html
link_directories โ€” CMake 3.0.2 Documentation
Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command.
๐ŸŒ
ICS
ics.com โ€บ blog โ€บ find-and-link-libraries-cmake
Find and Link Libraries with CMake | ICS
June 21, 2023 - To do this, we call target_link_libraries. Libraries can be linked PUBLIC, PRIVATE or INTERFACE. PUBLIC: Used by the library and its consumers. PRIVATE: Only needed by the library. INTERFACE: Not used by the library but needed for consumers. After the edits our main CMakeLists.txt will look like this:
๐ŸŒ
CMake
cmake.org โ€บ cmake โ€บ help โ€บ latest โ€บ prop_tgt โ€บ LINK_WHAT_YOU_USE.html
LINK_WHAT_YOU_USE โ€” CMake 4.2.3 Documentation
Then the command specified by CMAKE_LINK_WHAT_YOU_USE_CHECK will run after the target is linked to check the binary for unnecessarily-linked shared libraries. ... For now, it is only supported for ELF platforms and is only applicable to executable and shared or module library targets.