将qmake转换为CMake [英] Convert qmake into CMake

查看:130
本文介绍了将qmake转换为CMake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CMake的新手,但是我曾经使用qmake.在我的qmake中,我具有以下用于在项目文件夹内的名为bin的文件夹内添加静态库的信息:

I am new to CMake, but I used to use qmake. In my qmake, I have the following for adding a static library that is inside a folder called bin, inside the project folder:

QT -= gui
QT += core

CONFIG += c++11 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
        main.cpp
macx: LIBS += -L$$PWD/bin/lib/ -lnanomsg
INCLUDEPATH += $$PWD/bin/include
DEPENDPATH += $$PWD/bin/include
macx: PRE_TARGETDEPS += $$PWD/bin/lib/libnanomsg.a

对应的CMake语法是什么?

What is the corresponding CMake syntax?

我尝试了以下操作:

INCLUDE_DIRECTORIES(./bin/include)
LINK_DIRECTORIES($(CMAKE_SOURCE_DIR)/bin/lib/)
TARGET_LINK_LIBRARIES(nanomsg)

但是我收到无法为不是由该项目构建的nanomsg指定目标链接库"错误.我建立了与另一个项目对齐的库.

but I get "can not specify target link library for nanomsg which is not build by this project" error. I built the library aligned from another project.

删除 target_link_libraries 时,出现未定义符号的链接器错误.

When I remove the target_link_libraries, I get linker errors for undefined symbols.

[更新:这是最终可用的CMake文件]

[Updated: here is the final working CMake file]

cmake_minimum_required(VERSION 3.0.0)
project(cmake-linkstatic VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Core)
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries(${PROJECT_NAME} Qt5::Core)
#this line is not needed any more
#INCLUDE_DIRECTORIES(${CMAKE_CURRENT_LIST_DIR}/bin/include)
add_library(nanomsg STATIC IMPORTED)
# Specify the nanomsg library's location and include directories.
set_target_properties(nanomsg PROPERTIES
    IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/bin/lib/libnanomsg.a"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/bin/include"
)
# Define your executable CMake target.
# Link the nanomsg library to the executable target.
target_link_libraries(${PROJECT_NAME} nanomsg)

推荐答案

如果要在CMake中使用预构建的静态库,则可以声明 STATIC IMPORTED CMake目标:

If you want to use a pre-built static library in your CMake, you can declare a STATIC IMPORTED CMake target:

add_library(nanomsg STATIC IMPORTED)

# Specify the nanomsg library's location and its include directories.
set_target_properties(nanomsg PROPERTIES 
    IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/bin/lib/libnanomsg.a"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/bin/include"
)

在这里,我们使用 CMAKE_CURRENT_LIST_DIR 变量以访问当前CMakeLists.txt文件所在的目录.

Here, we use the CMAKE_CURRENT_LIST_DIR variable to access the directory in which the current CMakeLists.txt file resides.

target_link_libraries() 用于将一个库链接到另一个库(或可执行文件),因此语法应至少包含两个参数.例如,如果要将静态 nanomsg 库链接到可执行文件(例如 MyExecutable ),则可以在CMake中执行以下操作:

The target_link_libraries() is used to link one library to another library (or executable), so the syntax should include at least two arguments. For example, if you want to link the static nanomsg library to an executable (such as MyExecutable), you could do it in CMake like this:

add_library(nanomsg STATIC IMPORTED)

# Specify the nanomsg library's location and include directories.
set_target_properties(nanomsg PROPERTIES 
    IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/bin/lib/libnanomsg.a"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/bin/include"
)

# Define your executable CMake target.
add_executable(MyExecutable main.cpp)

# Link the nanomsg library to the executable target.
target_link_libraries(MyExecutable PUBLIC nanomsg)

这篇关于将qmake转换为CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆