To add link libraries to the source once you have them, use the target_link_libraries statement.

target_link_libraries(<target>... <item>... ...).Copy the code

Target is the target to which the link library is to be added, must be created by add_executable() or add_library(), and must not be an ALIAS target.

Item is the library to link to, which can be the name of the library, and the full (absolute) path to the library.

main.c

#include <stdio.h>

int main(a)
{
    hello();

    return 0;
}
Copy the code

Hello.c: hello.c: hello.c: hello.c: hello.c: hello.c: hello.c:

#include <stdio.h>

int hello(a)
{
    printf("hello CMake! \n");

    return 0;
}
Copy the code

CMakeLists. TXT content:

# CMake Minimum version requirements
cmake_minimum_required(VERSION 3.5)

# Project name
project(test_3)

# specify the build target
add_executable(test_3 main.c)

Add link library
Write the absolute path to the library file
target_link_libraries(test_3 /home/vistar/desktop/CMake/CMake_2/build/libtest_2.a)
Copy the code

The running results are as follows: