Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add a 3rd party C++ library to a Qt project?

How to add a 3rd party C++ library to a Qt project?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++ qt creator
13 Posts 5 Posters 945 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bonnie
    wrote on 26 Dec 2024, 07:43 last edited by Bonnie
    #2

    "add library" and ".pro" file are for qmake projects. Apparently your example program is a cmake project.
    You should either learn how to write CMakeLists.txt, or create a new qmake project.

    1 Reply Last reply
    1
    • M mpnix
      26 Dec 2024, 07:27

      I have downloaded the C++ exiftool to explore image metadata. I want to test that by running the example program as a Qt C++ desktop application. The program won't compile because it can't find the necessary #include files.
      Google tells me there is supposed to be an option of right-click on the project and "add library". That option does not show up.projectaddlib.jpg
      Other threads on this forum talk about adding statements to my ".pro" file. They imply that file can be found in the project's folder tree. I can't find one in any of my projects' trees.!
      projecttree.jpg
      projectbuilddir.jpg

      Someone please help a newbie in this platform!
      qtcreator.jpg

      P Online
      P Online
      Pl45m4
      wrote on 26 Dec 2024, 13:55 last edited by
      #3

      @mpnix

      As @Bonnie said, all three of the projects we can observe are CMake projects. Only Qt Creator projects have .pro files.
      So you can build the CMake library and the question is how to link it to your other Qt project?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      M 1 Reply Last reply 26 Dec 2024, 20:37
      0
      • P Pl45m4
        26 Dec 2024, 13:55

        @mpnix

        As @Bonnie said, all three of the projects we can observe are CMake projects. Only Qt Creator projects have .pro files.
        So you can build the CMake library and the question is how to link it to your other Qt project?

        M Offline
        M Offline
        mpnix
        wrote on 26 Dec 2024, 20:37 last edited by
        #4

        @Pl45m4 At this stage the project won't compile because there are source files (.h and .cpp) missing from the compiler's search path. What I don't understand is how to tell the compiler to go look in the exiftool's files. From @Bonnie's comment I assume that is done in the CMakeLists.txt file but I haven't found instructions for building that file - perhaps that's my lack of C/C++ experience rather than Qt experience. Either way, any pointers would be appreciated.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on 26 Dec 2024, 21:20 last edited by ChrisW67
          #5

          You will need to adjust the paths in the two target_*_directories entries

          # Add the exiv2 library into the build of project 'exifexample'
          target_include_directories(exifexample
              PRIVATE
              /opt/exiv2/include
          )
          target_link_directories(exifexample
              PRIVATE
              /opt/exiv2/lib
          )
          target_link_libraries(exifexample
              PRIVATE
              exiv2
          )
          
          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpnix
            wrote on 27 Dec 2024, 00:01 last edited by
            #6

            Thank you all for your guidance.
            I can make an intelligent guess at the meaning of those entries but they may not be complete. However, now I know that it's all "cmake" related, this would seem to be the place to go (https://cmake.org/cmake/help/book/mastering-cmake/index.html) for complete answers and understanding.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpnix
              wrote on 27 Dec 2024, 11:01 last edited by
              #7

              @ChrisW67 I'm running on Windows, not Linux, so the path and executable names are different.
              I installed (a year or so ago) the Windows binary exiftool and it runs from the command prompt giving sensible results. That lives in c:\program files\exiftool as exiftool.exe.
              I also downloaded (a few days ago) the exiftool for C++ which unpacks to

              c:\Users\murra\AppData\Local\Programs\cpp_exiftool
              

              ...with four subdirectories: The example I wish to run is in "example", the three ".h" files are in "inc" and the corresponding three ".cpp" files are in "src".
              So my first cut at the new cmakefile.txt is:

              cmake_minimum_required(VERSION 3.16)
              
              project(exifexample LANGUAGES CXX)
              
              set(CMAKE_CXX_STANDARD 17)
              set(CMAKE_CXX_STANDARD_REQUIRED ON)
              
              add_executable(exifexample main.cpp)
              
              include(GNUInstallDirs)
              install(TARGETS exifexample
                  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
              )
              target_include_directories(exifexample
                  PRIVATE
                  C:/Users/murra/AppData/Local/Programs/cpp_exiftool/inc
                  PRIVATE
                  C:/Users/murra/AppData/Local/Programs/cpp_exiftool/src
              )
              target_link_directories(exifexample
                  PRIVATE
                  "C:/Program Files/exiftool"
              )
              

              This fails to compile. The compiler reports an

              unresolved reference to `ExifTool::ExifTool(char const*, char const*)'
              

              in my main.cpp source code (the example).

              Note that I have removed the "target_link_libraries stanza, for now at least. I tried pointing it to the directory containing the binary exiftool.exe but it had no effect - positive or negative.

              Any idea what's missing?

              C 1 Reply Last reply 27 Dec 2024, 11:43
              0
              • M mpnix
                27 Dec 2024, 11:01

                @ChrisW67 I'm running on Windows, not Linux, so the path and executable names are different.
                I installed (a year or so ago) the Windows binary exiftool and it runs from the command prompt giving sensible results. That lives in c:\program files\exiftool as exiftool.exe.
                I also downloaded (a few days ago) the exiftool for C++ which unpacks to

                c:\Users\murra\AppData\Local\Programs\cpp_exiftool
                

                ...with four subdirectories: The example I wish to run is in "example", the three ".h" files are in "inc" and the corresponding three ".cpp" files are in "src".
                So my first cut at the new cmakefile.txt is:

                cmake_minimum_required(VERSION 3.16)
                
                project(exifexample LANGUAGES CXX)
                
                set(CMAKE_CXX_STANDARD 17)
                set(CMAKE_CXX_STANDARD_REQUIRED ON)
                
                add_executable(exifexample main.cpp)
                
                include(GNUInstallDirs)
                install(TARGETS exifexample
                    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
                )
                target_include_directories(exifexample
                    PRIVATE
                    C:/Users/murra/AppData/Local/Programs/cpp_exiftool/inc
                    PRIVATE
                    C:/Users/murra/AppData/Local/Programs/cpp_exiftool/src
                )
                target_link_directories(exifexample
                    PRIVATE
                    "C:/Program Files/exiftool"
                )
                

                This fails to compile. The compiler reports an

                unresolved reference to `ExifTool::ExifTool(char const*, char const*)'
                

                in my main.cpp source code (the example).

                Note that I have removed the "target_link_libraries stanza, for now at least. I tried pointing it to the directory containing the binary exiftool.exe but it had no effect - positive or negative.

                Any idea what's missing?

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 27 Dec 2024, 11:43 last edited by
                #8

                @mpnix said in How to add a 3rd party C++ library to a Qt project?:

                Any idea what's missing

                You can not link against executable, just dll through import libraries.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  mpnix
                  wrote on 27 Dec 2024, 21:05 last edited by
                  #9

                  I figured that was a silly thing to be doing. I would have thought that I'd need to point to a library containing the object output of the compiler to link the various bits together after separate compilation.
                  But I still cannot get around the unresolved reference in the compiler.
                  The ExifTool class is defined in the relevant cpp file in the src directory and that's referenced in the include_directories statement.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mpnix
                    wrote on 27 Dec 2024, 21:50 last edited by
                    #10

                    I'm beginning to think it's a bit more complicated than just the CMakeLists.txt file.
                    The download includes instructions to run "make" to build everything but I don't have "make" on my system. However, reading through the make file I deduced the instruction required to be:

                    C:\Users\murra\AppData\Local\Programs\cpp_exiftool>g++ -g -oexample/example2.o  -I inc  example/example2.cpp
                    

                    But that gives a similar, but more extensive list, of unresolved references for pretty much anything which should be in ExifTool:

                    C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Temp\ccNlX3g5.o: in function `main':
                    C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:36:(.text+0xc1): undefined reference to `ExifTool::ExifTool(char const*, char const*)'
                    C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:39:(.text+0xe7): undefined reference to `ExifTool::SetNewValue(char const*, char const*, int)'
                    C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:40:(.text+0x10a): undefined reference to `ExifTool::SetNewValue(char const*, char const*, int)'
                    C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:43:(.text+0x132): undefined reference to `ExifTool::WriteInfo(char const*, char const*, TagInfo*)'
                    C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:46:(.text+0x148): undefined reference to `ExifTool::Complete(double)'
                    C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:52:(.text+0x169): undefined reference to `ExifTool::GetSummary(char const*)'
                    C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:53:(.text+0x180): undefined reference to `ExifTool::GetSummary(char const*)'
                    collect2.exe: error: ld returned 1 exit status
                    
                    P 1 Reply Last reply 28 Dec 2024, 00:12
                    0
                    • M mpnix
                      27 Dec 2024, 21:50

                      I'm beginning to think it's a bit more complicated than just the CMakeLists.txt file.
                      The download includes instructions to run "make" to build everything but I don't have "make" on my system. However, reading through the make file I deduced the instruction required to be:

                      C:\Users\murra\AppData\Local\Programs\cpp_exiftool>g++ -g -oexample/example2.o  -I inc  example/example2.cpp
                      

                      But that gives a similar, but more extensive list, of unresolved references for pretty much anything which should be in ExifTool:

                      C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Temp\ccNlX3g5.o: in function `main':
                      C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:36:(.text+0xc1): undefined reference to `ExifTool::ExifTool(char const*, char const*)'
                      C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:39:(.text+0xe7): undefined reference to `ExifTool::SetNewValue(char const*, char const*, int)'
                      C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:40:(.text+0x10a): undefined reference to `ExifTool::SetNewValue(char const*, char const*, int)'
                      C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:43:(.text+0x132): undefined reference to `ExifTool::WriteInfo(char const*, char const*, TagInfo*)'
                      C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:46:(.text+0x148): undefined reference to `ExifTool::Complete(double)'
                      C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:52:(.text+0x169): undefined reference to `ExifTool::GetSummary(char const*)'
                      C:/Program Files/mingw/mingw32/bin/../lib/gcc/i686-w64-mingw32/14.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\murra\AppData\Local\Programs\cpp_exiftool/example/example2.cpp:53:(.text+0x180): undefined reference to `ExifTool::GetSummary(char const*)'
                      collect2.exe: error: ld returned 1 exit status
                      
                      P Online
                      P Online
                      Pl45m4
                      wrote on 28 Dec 2024, 00:12 last edited by Pl45m4
                      #11

                      @mpnix said in How to add a 3rd party C++ library to a Qt project?:

                      I'm beginning to think it's a bit more complicated than just the CMakeLists.txt file.

                      No it's not. When you do it properly, it should work

                      The download includes instructions to run "make" to build everything but I don't have "make" on my system.

                      Make(file) and CMake are two similar but different approaches.
                      Since you have CMake projects, you better stick to CMake than fiddling around with manual commands and Makefile.

                      Edit:

                      Your Exiftool CPP man page says, that it's just a wrapper for Exiftool... So you need to install it anyway and link the Exiftool wrapper to your Qt app to use it from there.

                      • https://exiftool.org/cpp_exiftool/

                      Requirements

                      The exiftool application must exist on the system. This interface should be platform independent, and has been tested on Mac OS X, Linux, and Windows (Cygwin).


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      M 1 Reply Last reply 28 Dec 2024, 11:09
                      0
                      • P Pl45m4
                        28 Dec 2024, 00:12

                        @mpnix said in How to add a 3rd party C++ library to a Qt project?:

                        I'm beginning to think it's a bit more complicated than just the CMakeLists.txt file.

                        No it's not. When you do it properly, it should work

                        The download includes instructions to run "make" to build everything but I don't have "make" on my system.

                        Make(file) and CMake are two similar but different approaches.
                        Since you have CMake projects, you better stick to CMake than fiddling around with manual commands and Makefile.

                        Edit:

                        Your Exiftool CPP man page says, that it's just a wrapper for Exiftool... So you need to install it anyway and link the Exiftool wrapper to your Qt app to use it from there.

                        • https://exiftool.org/cpp_exiftool/

                        Requirements

                        The exiftool application must exist on the system. This interface should be platform independent, and has been tested on Mac OS X, Linux, and Windows (Cygwin).

                        M Offline
                        M Offline
                        mpnix
                        wrote on 28 Dec 2024, 11:09 last edited by
                        #12

                        @Pl45m4 I agree with almost everything you said with the possible exception of the first part since I can't compile any of the CPP examples by any method. That said, that is almost certainly an issue between my chair and my computer (called lack of prior experience).

                        So, what does "do it properly" mean in reality? Sticking with CMake seems a first wise step (I only looked at the "Make" stuff to see if it gave me any different outcome - it didn't).

                        So, given

                        • CPP wrapper is installed in %appdata%\local\programs\cpp_exiftool with

                        • the ".h" files in the "inc" subdirectory, the ".cpp" files in the "src" subdirectory, and the example files in the "example" directory and

                        • the exiftool application is installed in %programfiles%\exiftool as "exiftool.exe" (the only item in the directory)

                        What does a "proper" CMakeLists.txt file look like?
                        The current version (and I realise the target_link_directories stanza is wrong pointing to an executable library instead of an object library but I have no object library that I know of - the Windows exiftool download as a ready to run exe file) looks like this:

                        cmake_minimum_required(VERSION 3.16)
                        
                        project(exifexample LANGUAGES CXX)
                        
                        set(CMAKE_CXX_STANDARD 17)
                        set(CMAKE_CXX_STANDARD_REQUIRED ON)
                        
                        add_executable(exifexample main.cpp)
                        
                        include(GNUInstallDirs)
                        install(TARGETS exifexample
                            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                            RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
                        )
                        target_include_directories(exifexample
                            PRIVATE
                            C:/Users/murra/AppData/Local/Programs/cpp_exiftool/src
                            PRIVATE
                            C:/Users/murra/AppData/Local/Programs/cpp_exiftool/inc
                        )
                        target_link_directories(exifexample
                            PRIVATE
                            "C:/Program Files/exiftool"
                        )
                        

                        The first undefined reference refers to ExifTool::ExifTool(char const*, char const*).
                        The ExifTool.cpp code, in the src directory listed in the target_include_directories above, creates an ExifTool class containing this constructor:

                        //==============================================================================
                        // ExifTool object constructor
                        //------------------------------------------------------------------------------
                        // Inputs: exec - path name to executable file (ie. "perl" or "exiftool")
                        //         arg1 - optional first argument (ie. "exiftool" if exec="perl")
                        ExifTool::ExifTool(const char *exec, const char *arg1)
                                : mWatchdog(-1), mWriteInfo(NULL), mCmdQueue(NULL), mCmdQueueLen(0),
                                  mCmdQueueSize(0), mLastComplete(0), mCmdNum(0), mWaitTime(1000)
                        {...
                        

                        To my inexperienced eye, this appears to be a defined reference which is in a file from which the compiler should be able to import.

                        What am I missing here?
                        I have the exiftool installed as a working and tested executable on my system.
                        I have the cpp_exiftool code downloaded as source to be built into my project.

                        P 1 Reply Last reply 28 Dec 2024, 17:25
                        0
                        • M mpnix
                          28 Dec 2024, 11:09

                          @Pl45m4 I agree with almost everything you said with the possible exception of the first part since I can't compile any of the CPP examples by any method. That said, that is almost certainly an issue between my chair and my computer (called lack of prior experience).

                          So, what does "do it properly" mean in reality? Sticking with CMake seems a first wise step (I only looked at the "Make" stuff to see if it gave me any different outcome - it didn't).

                          So, given

                          • CPP wrapper is installed in %appdata%\local\programs\cpp_exiftool with

                          • the ".h" files in the "inc" subdirectory, the ".cpp" files in the "src" subdirectory, and the example files in the "example" directory and

                          • the exiftool application is installed in %programfiles%\exiftool as "exiftool.exe" (the only item in the directory)

                          What does a "proper" CMakeLists.txt file look like?
                          The current version (and I realise the target_link_directories stanza is wrong pointing to an executable library instead of an object library but I have no object library that I know of - the Windows exiftool download as a ready to run exe file) looks like this:

                          cmake_minimum_required(VERSION 3.16)
                          
                          project(exifexample LANGUAGES CXX)
                          
                          set(CMAKE_CXX_STANDARD 17)
                          set(CMAKE_CXX_STANDARD_REQUIRED ON)
                          
                          add_executable(exifexample main.cpp)
                          
                          include(GNUInstallDirs)
                          install(TARGETS exifexample
                              LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                              RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
                          )
                          target_include_directories(exifexample
                              PRIVATE
                              C:/Users/murra/AppData/Local/Programs/cpp_exiftool/src
                              PRIVATE
                              C:/Users/murra/AppData/Local/Programs/cpp_exiftool/inc
                          )
                          target_link_directories(exifexample
                              PRIVATE
                              "C:/Program Files/exiftool"
                          )
                          

                          The first undefined reference refers to ExifTool::ExifTool(char const*, char const*).
                          The ExifTool.cpp code, in the src directory listed in the target_include_directories above, creates an ExifTool class containing this constructor:

                          //==============================================================================
                          // ExifTool object constructor
                          //------------------------------------------------------------------------------
                          // Inputs: exec - path name to executable file (ie. "perl" or "exiftool")
                          //         arg1 - optional first argument (ie. "exiftool" if exec="perl")
                          ExifTool::ExifTool(const char *exec, const char *arg1)
                                  : mWatchdog(-1), mWriteInfo(NULL), mCmdQueue(NULL), mCmdQueueLen(0),
                                    mCmdQueueSize(0), mLastComplete(0), mCmdNum(0), mWaitTime(1000)
                          {...
                          

                          To my inexperienced eye, this appears to be a defined reference which is in a file from which the compiler should be able to import.

                          What am I missing here?
                          I have the exiftool installed as a working and tested executable on my system.
                          I have the cpp_exiftool code downloaded as source to be built into my project.

                          P Online
                          P Online
                          Pl45m4
                          wrote on 28 Dec 2024, 17:25 last edited by Pl45m4
                          #13

                          @mpnix said in How to add a 3rd party C++ library to a Qt project?:

                          So, what does "do it properly" mean in reality? Sticking with CMake seems a first wise step (I only looked at the "Make" stuff to see if it gave me any different outcome - it didn't).

                          After taking a closer look, it seem to require a Cygwin environment under Windows, which is also stated here:

                          @Pl45m4 said in How to add a 3rd party C++ library to a Qt project?:

                          Requirements

                          The exiftool application must exist on the system. This interface should be platform independent, and has been tested on Mac OS X, Linux, and Windows (Cygwin).

                          So it's only "pseudo" platform-indepentent.
                          Can't tell you much about Cygwin... for Unix/Linux optimized stuff I use Linux directly and not Windows.

                          You could try another, more Windows friendly, Exif library. For example this one:

                          • https://github.com/exiv2/exiv2

                          As far as I can see, it also supports CMake builds "out-of-the-box"... so no MakeFile converting etc. etc.


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          1 Reply Last reply
                          0

                          11/13

                          28 Dec 2024, 00:12

                          • Login

                          • Login or register to search.
                          11 out of 13
                          • First post
                            11/13
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved