最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How to properly add external cmake library (OpenXLSX) to a Qt creator qml project? - Stack Overflow

programmeradmin6浏览0评论

I'm q newbie and i searched for the solution across the web making some "minor" progress but i'm pretty sure that i'm do things wrongly and there are very few video and tutorial about that, or maybe i didn't find it?

I'm experimenting with Qt Creator 15.0.1 adn Qt 6.6.3 writing some c++ qml project to learn this world a little bit, i'm using Linux Mint.

Now i'm trying to use the OpenXLSX library and following the official GitHub i "simply" need to "[...] add the OpenXLSX folder as a subdirectory to your own project [...] add the OpenXLSX folder as a subdirectory to the source tree of your own project". But i'm not sure how to do it properly.

I've done some test and managed to made the library work 2 times,

in the first attempt i added the header and the source file from the folder directly to the test project. With this method i had to edit every #include in all the source and header of the OpenXLSX library to make it work to point to the exact file... Worked well, no red error or warning mark but i think this is not the right way to do it.

So i tried to "add an existing project" pointing to the library cmakelists file. This one worked quite well and to make it work i simply added to my cmakelists file this code

target_link_libraries(appTest
    PRIVATE Qt6::Quick OpenXLSX::OpenXLSX
)

#and

add_subdirectory(OpenXLSX)

This one worked, compiling and running well, but i got a lot yellow mark issue warning for "implicit conversion change signedness" "[-Wsign conversion] [-Wshorten-64-to-32] or [-Wdefaulted-function-deleted]

searched a lot on the web, someone suggest to use an "add library" command in qt creator but i can't find this voice in any of the menu.

What are the proper way to add this Library (and i suppose every other library i need) to an existing cmake project? THIS answer is 15 years old so i think that things are changed in the mean time and also other answer reefer to .pro file and "add library" voice that i don't seems to have in my Qt Creator.

I'm q newbie and i searched for the solution across the web making some "minor" progress but i'm pretty sure that i'm do things wrongly and there are very few video and tutorial about that, or maybe i didn't find it?

I'm experimenting with Qt Creator 15.0.1 adn Qt 6.6.3 writing some c++ qml project to learn this world a little bit, i'm using Linux Mint.

Now i'm trying to use the OpenXLSX library and following the official GitHub i "simply" need to "[...] add the OpenXLSX folder as a subdirectory to your own project [...] add the OpenXLSX folder as a subdirectory to the source tree of your own project". But i'm not sure how to do it properly.

I've done some test and managed to made the library work 2 times,

in the first attempt i added the header and the source file from the folder directly to the test project. With this method i had to edit every #include in all the source and header of the OpenXLSX library to make it work to point to the exact file... Worked well, no red error or warning mark but i think this is not the right way to do it.

So i tried to "add an existing project" pointing to the library cmakelists file. This one worked quite well and to make it work i simply added to my cmakelists file this code

target_link_libraries(appTest
    PRIVATE Qt6::Quick OpenXLSX::OpenXLSX
)

#and

add_subdirectory(OpenXLSX)

This one worked, compiling and running well, but i got a lot yellow mark issue warning for "implicit conversion change signedness" "[-Wsign conversion] [-Wshorten-64-to-32] or [-Wdefaulted-function-deleted]

searched a lot on the web, someone suggest to use an "add library" command in qt creator but i can't find this voice in any of the menu.

What are the proper way to add this Library (and i suppose every other library i need) to an existing cmake project? THIS answer is 15 years old so i think that things are changed in the mean time and also other answer reefer to .pro file and "add library" voice that i don't seems to have in my Qt Creator.

Share Improve this question edited Apr 2 at 5:26 hyde 62.9k22 gold badges125 silver badges179 bronze badges asked Mar 22 at 12:33 MorenoMoreno 234 bronze badges 7
  • 2 You're doing it correctly, at least as far as one can tell. What makes you think linking the library would cause the warning? The reason is most likely unrelated and caused by your code. You probably convert the result of a function that returns a std::size_t to an int or something like that. You ask about several questions at the same time, please edit and focus on one of them. – Friedrich Commented Mar 22 at 16:44
  • You also seem to be confused by the fact that Qt Creator can use its own QMake .pro files as well as CMake. The latter is recommended. Both Qt Creator and CMake are well documented, read it. – Friedrich Commented Mar 22 at 16:48
  • Hi fredrich, it's highly probable that i'm confused about cmake and qmake but i use cmake, i only read a bit about qmake and honestly i didn't know anything about. The fact is that the project where i edited all the include in every library file don't give me any warning like he second one does, this can't be related to my code since both project are totally void with only the classic "hello world" and the include to adding the OpenXLSX library, there are basically no code to break in the project. So i suppose that the right method in the number 2? – Moreno Commented Mar 22 at 17:22
  • 1 I think i've not described well the point of the question. The code it's literally the basic "hello word" code that the software generate when you open a project. nothing to show except for the cmakelists row posted before and an include. The only thing i can post can maybe be the GitHub to the OpenXLSX library but i just want to know what are the safe way to add that project. Right click on the project name and "Add existing project"? "New subprojects"? or click the source directory and select "Add existing Files/Directory"? – Moreno Commented Mar 22 at 20:13
  • 1 What comments above request is, and to make this a good question, you should provide the entire CMakelists.text in the question, as well as minimal "hello world" main.cpp to reproduce the warnings or other problems. Also add the compile output from Qt Creator. It's good idea to add -v to build tool options in Qt Creator Project view - Build Steps - Tool Arguments, so CMake gives that option to Ninja (probably the same with Make). This not very many lines, so doing this should not be any problem. – hyde Commented Apr 2 at 5:28
 |  Show 2 more comments

2 Answers 2

Reset to default 0

It is quite common that library header files trigger warnings. This is usually ok for system headers which are supplied with the compiler. For 3rd party libraries, it tells either of you using untested platform, of sloppy coding where the library developer does not care about warnings, or even of bugs.


You need to mark the include directories of that library as system include directory, using SYSTEM flag in target_include_directories

Example adapted from this discussion, it should be something like this:

target_include_directories(appTest
    SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/OpenXLSX/include
)

Another solution is to fork the library in GitHub, fix the warnings, and use your own fork. BSD-3 license should make forking and modifying non-problematic even for commercial/proprietary code. If you can be bothered, doing this with otherwise minimal changes, then creating a pull request for the upstream to take in your fixes, is highly recommended, as that is how open source software improves.

I want to reply to my own question just to update this post.
Sorry if i'm writing only after many days of silence, i readed all the suggestion and i want to say thanks to everyone!

I've experimented further with the integration of the library (and with many other things), i successfully used "Method 2" but i like to maintain my project as clean as possible so i didn't like that my folder/file tree was filled by numerous header and source file.

I ended up compiling the library into a shared library .so in my case and use it in my project in a much cleaner way (imho) basically using only dll and header file.
Warning keep appearing but for now it's fine like this.

发布评论

评论列表(0)

  1. 暂无评论