本文介绍了cmake导入存在SimpleAmqp库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的CMakeList.txt看起来像这样。

Now my CMakeList.txt looking like this.

cmake_minimum_required(VERSION 3.6)
project(RabbitMQClient)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
include_directories(src)
include_directories(dependentFile)
add_executable(RabbitMQClient ${SOURCE_FILES})
find_library(SimpleAmqpClient SimpleAmqpClient.2.lib PATHS ./SimpleAmqpClientLib/SimpleAmqpClient.2.lib)
set(IMPORTED_IMPLIB ./SimpleAmqpClientLib)
target_link_libraries(RabbitMQClient PUBLIC SimpleAmqpClient)

在链接RabbitMQClient时抱怨。

when Linking the RabbitMQClient it complains.

在项目中使用 SimpleAmqpClient 库,但是对 cmake 不太熟悉,不确定 find_library IMPORTED_IMPLIB PUBLIC target_link_libraries

I want to use the SimpleAmqpClient library in the project,however not quite familiar with cmake not sure the find_library,IMPORTED_IMPLIB,PUBLIC in target_link_libraries was correctly used.Any help would be appreciate.

推荐答案

您搞砸了变量目标属性

正确使用导入的库进行链接将是:

Proper usage of IMPORTED libraries for linking would be:

# This command sets *variable* SimpleAmqpClient_LIBRARY
find_library(SimpleAmqpClient_LIBRARY SimpleAmqpClient.2.lib
    PATHS ${CMAKE_SOURCE_DIR}/SimpleAmqpClientLib # Specify a *directory*, not a library *file*
)
# Next, create an IMPORTED *target*
add_library(SimpleAmqpClient SHARED IMPORTED)
# And set IMPORTED_LIB *property* for this target
set_target_properties(SimpleAmqpClient PROPERTIES IMPORTED_LIB ${SimpleAmqpClient_LIBRARY})
# Then use library *target* for linking with
target_link_libraries(RabbitMQClient PUBLIC SimpleAmqpClient)

但是,可以做一些简化:

However, there some simplifications which could be done:


  1. 通常, find_lib如果您不知道库文件的完整路径,则使用rary 。例如,在不同的计算机上,其目录可能不同,或者在不同的平台上,其前缀 / 扩展名也可能不同。

  1. Normally, find_library is used when you don't know complete path to the library file. E.g., its directory may be different on different machines, or its prefix/extension may be different on different plaforms.

如果您知道库的完整路径,则直接使用此路径(例如,将其分配给变量)。

If you know complete path to the library, just use this path directly (e.g., assign it to the variable).

通常,属性 IMPORTED_LOCATION 用于指定要链接的库。当链接不需要库文件( .dll IMPORTED_LIB 专用于Windows .dll s $ c>),但另一些( .lib )。

Normally, property IMPORTED_LOCATION is used for specify library to link with. Property IMPORTED_LIB is specific for Windows .dlls, when linking requires not a library file (.dll), but some other one (.lib).

但是,CMake完全理解。即使对于Windows .dll s也位于 IMPORTED_LOCATION 属性中的lib 文件,因此您的代码无需区分 SHARED 其他Windows库:在所有情况下都只需使用 IMPORTED_LOCATION 属性。

However, CMake perfectly understands .lib file in IMPORTED_LOCATION property even for Windows .dlls, so your code need not distinguish SHARED Windows libraries from others: just use IMPORTED_LOCATION property in all cases.

简化的版本的代码:

# Create an IMPORTED library *target*
add_library(SimpleAmqpClient IMPORTED)
# Set IMPORTED_LOCATION *property* for this target
set_target_properties(SimpleAmqpClient PROPERTIES
    IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/SimpleAmqpClientLib/SimpleAmqpClient.2.lib)
# Then use library *target* for linking with
target_link_libraries(RabbitMQClient PUBLIC SimpleAmqpClient)

这篇关于cmake导入存在SimpleAmqp库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 02:20