Convert qmake into CMake

会有一股神秘感。 提交于 2021-01-28 11:37:07

问题


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

What is the corresponding CMake syntax?

I tried the following:

INCLUDE_DIRECTORIES(./bin/include)
LINK_DIRECTORIES($(CMAKE_SOURCE_DIR)/bin/lib/)
TARGET_LINK_LIBRARIES(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.

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

[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)

回答1:


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"
)

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

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)


来源:https://stackoverflow.com/questions/60061299/convert-qmake-into-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!