简体   繁体   English

cmake 与 Qt 共享库

[英]cmake shared lib with Qt

I created two default projects with QtCreator v6.0.1: MyApp and MyLib for Linux(Ubuntu).我使用 QtCreator v6.0.1 创建了两个默认项目:MyApp 和 MyLib for Linux(Ubuntu)。 I changed the folders structure to我将文件夹结构更改为

MyApp
├──build
│   ├──Debug
|   └──Release
├──src
├──libs
|   └──MyLib
|        ├──build
|        ├──src
|        ├──include
|        └──CMakeLists.txt
├──include
├──src
└──CMakeLists.txt

What kind of changes I need to make with both of CMakeLists.txt to make MyLib as shared library and use it in MyApp?我需要对 CMakeLists.txt 进行哪些更改才能使 MyLib 作为共享库并在 MyApp 中使用它?

First of all, as already mentioned, you'll need to add the MyLib directory with add_subdirectory in the top-level CMakeLists.txt:首先,如前所述,您需要在顶级 CMakeLists.txt 中使用add_subdirectory添加MyLib目录:

    ...
    add_subdirectory(libs/MyLib)
    ...

Then, defining a shared/dynamic library with CMake is pretty straight forward.然后,使用 CMake 定义共享/动态库非常简单。 You use add_library providing the name of the library, the source files and some flags.您使用add_library提供库的名称、源文件和一些标志。 By default it will create a static library, but with the keyword SHARED you can change that:默认情况下,它将创建一个 static 库,但使用关键字SHARED您可以更改它:

    add_library(MyLib SHARED
                MySource1.cpp
                MySource2.cpp
                ...
                )

Have a look here: https://cmake.org/cmake/help/latest/command/add_library.html看看这里: https://cmake.org/cmake/help/latest/command/add_library.html

By the way, all generated code (including the library) will end up in the one and only build directory (which you either specify with cmake -B $BUILD_DIR or which is the working directory for running cmake).顺便说一句,所有生成的代码(包括库)都将在唯一的构建目录中结束(您可以使用cmake -B $BUILD_DIR指定,或者是运行 cmake 的工作目录)。 So you won't have an additional one in your libs/MyLib folder.因此,您的 libs/MyLib 文件夹中不会再有一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM