How to add a 3rd party C++ library to a Qt project?
-
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.
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
Someone please help a newbie in this platform!
-
@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.
-
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 )
-
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. -
@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 toc:\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?
-
@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.
-
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. -
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
-
@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.
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).
-
@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. -
-
@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:
As far as I can see, it also supports CMake builds "out-of-the-box"... so no MakeFile converting etc. etc.