CMake Static Library linking (Precompiled .a)

705 Views Asked by At

I've been dealing with this problem day and night for a week. I've read every page on Google, Stackoverflow and Github.

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED OFF)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(EDKPOC)

add_library(BCXConfiguration STATIC IMPORTED)

set_target_properties(BCXConfiguration PROPERTIES
        IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/main/liBCXConfiguration.a"
        INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/main/include")

link_directories(${CMAKE_CURRENT_LIST_DIR})

target_link_libraries(EDKPOC BCXConfiguration)

It gives this error when I run CMake:

CMake Error at CMakeLists.txt:20 (target_link_libraries):
  Cannot specify link libraries for target "EDKPOC" which is not built
  by this project.
1

There are 1 best solutions below

2
Kevin On

The target_link_libraries() command is used to link a given target with its dependent targets. You specify EDKPOC as the first argument to the command; this is your project name, but it is not a valid target. The only target you've specified in your example CMake file is BCXConfiguration. If you want to link this static library to another target (e.g. MyOtherLibraryTarget) elsewhere in your CMake project, your syntax could look something like this:

target_link_libraries(MyOtherLibraryTarget PUBLIC BCXConfiguration)

If you don't have other targets in your CMake hierarchy, then this command is unnecessary and can be left out.