简体   繁体   中英

Add and use an external library in Cmake for a Qt project

I am trying to make use of libmaia in a Qt project using C++.

Since I'm from a JavaScript background, all of the answers regarding setup of an external library in Qt are going way over my head.

This library libmaia has docs for installation but it only points out how to install using qMake. I am using CMake in my project.

What I've tried:

  • Cloned the project and copied the files over to the Qt project
  • Tried adding a find_library() call in the CMake, which fails.
  • So, I tried the target_link_libraries() entry for the project.

My understanding is that I am missing the build step of the project to generate a .a or a .so file.

How do we conclude that an external library will only work with QMake or CMake?

Any help / resources to read up / GitHub examples of the setup would be gladly appreciated.

Also, if someone can simplify my understanding of the setup procedure, that would be great! Thanks!

"... and copied the files over to the Qt project" - No, don't. Remove them from there. You should build and install the library separately.

git clone git@github.com:wiedi/libmaia.git
cd libmaia

Then, the instruction to build is to do qmake + make - but that fails for me (with Qt6), so I used cmake .

If you build libmaia for yourself only, you can install the library in a place where you store other libraries you build. It's common to place them in a directory named .local in the home directory. So mkdir ~/.local (or something like mkdir %USERPROFILE%\source\repos\.local on Windows) if you don't have such a directory already, then:

mkdir build
cd build
cmake --install-prefix ~/.local ..
# Windows: cmake --install-prefix %USERPROFILE%\source\repos\.local
make -j
cmake --install .

(Note: It's really .. for the first cmake and . for the second)

Possible output of the cmake --install. command:

-- Install configuration: ""
-- Installing: /home/ted/.local/lib/libmaia.a
-- Installing: /home/ted/.local/lib/cmake/libmaia/libmaiaTargets.cmake
-- Installing: /home/ted/.local/lib/cmake/libmaia/libmaiaTargets-noconfig.cmake
-- Installing: /home/ted/.local/lib/cmake/libmaia/libmaiaConfig.cmake
-- Installing: /home/ted/.local/lib/cmake/libmaia/libmaiaConfigVersion.cmake
-- Installing: /home/ted/.local/include/maia/maiaFault.h
-- Installing: /home/ted/.local/include/maia/maiaObject.h
-- Installing: /home/ted/.local/include/maia/maiaXmlRpcClient.h
-- Installing: /home/ted/.local/include/maia/maiaXmlRpcServerConnection.h
-- Installing: /home/ted/.local/include/maia/maiaXmlRpcServer.h

In your own project's CMakeLists.txt (replace /home/ted/.local or C:/Users/Ted/source/repos/.local ) with the path to your .local directory:

...
find_package(Qt5Network CONFIG REQUIRED)
find_package(Qt5Xml CONFIG REQUIRED)
find_package(libmaia CONFIG REQUIRED PATHS /home/ted/.local)
# or C:/Users/Ted/source/repos/.local

add_executable(Foo main.cpp)

target_link_libraries(Foo libmaia::maia Qt5::Network Qt5::Xml)

Then in your code:

#include "maia/maiaXmlRpcClient.h"
// and / or
#include "maia/maiaXmlRpcServer.h"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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