cmake + cpp: No such file or directory - c++

I've I'm trying to build this "Hello World" wxWidgets example on Linux, using the following cmake script:
cmake_minimum_required (VERSION 2.6)
project (wxL)
find_package(wxWidgets 3.0.0 REQUIRED
COMPONENTS base core net xml html adv qa richtext
)
file(GLOB SOURCES "src/*.cpp")
add_executable(wxL ${SOURCES})
Building the project yields this error:
src/wxL.cpp:3:10: fatal error: wx/wxprec.h: No such file or directory
The file specified in the include, wx/wxprec.h can be found on disk at this location:
/usr/include/wx-3.0/wx/wxprec.h
Furthermore, another program that I have built from source includes the same file (also using cmake) and builds just fine.
So, how do I use cmake to tell the compiler that the file should be included from somewhere in the system directories?
I know I'm missing something basic, but I can't figure out what.

Although you've found the package, your executable does not know anything about it.
For the executable to compile correctly, it needs to find header files for your package together with the .so / .a files. Following example should get you started:
include_directories(${wxWidgets_INCLUDE_DIRS})
add_executable(wxL <add-source-files-here>)
target_link_libraries(wxL ${wxWidgets_LIBRARIES}) // links wxWidgets libraries to your executable
Please note that using glob is not a recommended way of adding source files to your project.

Related

Undefined reference errors when trying to compile and simple ImageMagick program

I've been searching for a solution to this problem for a long time to no avail.
I am trying to compile this simple program:
#include <iostream>
#include <Magick++.h>
#include <Magick++/Image.h>
using namespace std;
using namespace Magick;
int main(int argc,char **argv) {
InitializeMagick("D:\\Programming\\CPPProjects\\NoteScripts\\Dependencies\\magick\\include");
Image image;
// image.read("arch");
// image.write("test.png");
}
Upon building, I get the following error:
CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1c): undefined reference to `Magick::InitializeMagick(char const*)'
CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x28): undefined reference to `Magick::Image::Image()'
CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x34): undefined reference to `Magick::Image::~Image()'
collect2.exe: error: ld returned 1 exit status
From what I can tell, this is a linker error but I have no idea where I am going wrong with linking the libs needed.
I installed ImageMagick on Windows 10 from the ImageMagick downloads page with this installer: ImageMagick-7.1.0-50-Q16-HDRI-x64-dll.exe
I then copied the lib files from the lib folder under the installation directory into my project and then copied the include folder under the installtion directory into my project.
Here is what the project hierarchy looks like (Source Directory is NoteScripts):
My CMakeLists.txt consists of:
cmake_minimum_required(VERSION 3.10)
set( CMAKE_CXX_COMPILER "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe")
set( CMAKE_C_COMPILER "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe" )
# set the project name
project("Notes")
include_directories(D:/Programming/CPPProjects/NoteScripts/Dependencies/magick/include)
# add the executable
add_executable(main main.cpp)
target_link_libraries(main D:/Programming/CPPProjects/NoteScripts/Dependencies/magick/lib/CORE_RL_Magick++_.lib)
target_link_libraries(main D:/Programming/CPPProjects/NoteScripts/Dependencies/magick/lib/CORE_RL_MagickCore_.lib)
target_link_libraries(main D:/Programming/CPPProjects/NoteScripts/Dependencies/magick/lib/CORE_RL_MagickWand_.lib)
If I comment out lines 9 and 10 where InitializeMagick() is called and where Image image is declared, the program compiles without error. I'm also aware that the order of the static libs listed out matters but trying out multiple combinations has resulted in the same error. I have also verfied the dependency order by sifting through the original source code and the reference path is Magick++ -> MagickCore -> MagickWand.
I am relatively new to the process of adding external dependencies to my C++ projects so this is unfamiliar territory (coming from languages with clean package managers). Any help as to how to fix this issue is greatly appreciated!
The typical (and easiest) way of handling dependencies in CMake is using its find_package command:
find_package(ImageMagick REQUIRED COMPONENTS MagickCore MagickWand Magick++)
// ...
target_link_libraries(main ${ImageMagick_LIBRARIES})
This method is available for ImageMagick with your CMake version. I'm not familiar with CMake on Windows, but find_package by default searches a number of standard (system) locations for the package's files. Since you have a custom setup, it should also be possible to specify a nonstandard search prefix to the command. Additionally, you could download external dependencies in a portable way with the FetchContent commands.
First of all, it is a pain to setup this thing if you are a newbie like me.
Now to the steps to dynamically link imagemagick libs with your C app:
go to https://github.com/ImageMagick/ImageMagick-Windows and follow the instructions there (Install Visual Studio dependencies - Clone the dependencies - Build configure.exe- Build ImageMagick)
in the step Build configure.exe, when running configure.exe, keep the default option selected when asked about the output library type (keep it set to dynamic)
in the Build ImageMagick step, when you open VisualDynamicMT.sln in visual studio, before you start the build, select all the solutions in the project, and right-click -> properties -> General -> C Language Standard -> choose Default (Legacy MSVC). After that, click on the top-most solution that contains all the other 196 solutions, and build it. watch the console for errors, I didn't get any errors with the configuration above.
After the latter step, go the VisualMagick folder (created from steps before), and you will see lib folder and bin folder. You're done, your dlls are in bin and your .lib file are in bin folder. Note that these files will be corresponding to build or release environments depending on what you selected in visual studio at the build step.
How do you use imagemagick now in your project regardless if you have imagemagick app installed on your pc or not? Lets create a new project in vscode, call it demo.
Create this project structure:
inside src you will put your C code.
inside deps create ImageMagick/lib and ImageMagick/bin and ImageMagick/include
inside ImageMagick/include place the same include files you said you got in your question.
inside ImageMagick/lib place the .lib files you got from above
inside ImageMagick/bin place the .dll files you got from above
now add this to your CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(demo-app C)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
file(GLOB SOURCE_FILES src/*.c)
add_executable(demo ${SOURCE_FILES})
include_directories(src)
# ImageMagick
if(WIN32)
add_definitions( -DMAGICKCORE_QUANTUM_DEPTH=16 )
add_definitions( -DMAGICKCORE_HDRI_ENABLE=0 )
include_directories(deps/ImageMagick/include)
target_link_directories(demo PRIVATE deps/ImageMagick/lib)
file(GLOB IMAGEMAGICK_LIBS deps/ImageMagick/lib/*.lib)
target_link_libraries(demo
"${IMAGEMAGICK_LIBS}"
)
add_custom_command(TARGET demo POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_SOURCE_DIR}/deps/ImageMagick/bin"
$<TARGET_FILE_DIR:demo>)
endif()
(The add_custom_command will copy the dlls to your executables path after every build)
Now write some magick code in you src directory.
ctrl + shift + A -> CMake select kit -> choose the Visual Studio community 2022 release -amd64 or change to what fits you if it doesn't work
ctrl + shift + A -> CMake Clean Rebuild
ctrl + shift + A -> CMake run without debugging

Cant compile OpenCV Source file with my project

This question is a continuation of one I asked before
I have a project that uses the librealsense library and OpenCV. librealsense works but I cannot include OpenCV in my project for the life of me. There is currently only 1 CPP file and 1 HPP file, this is the cmake file of the root
project(peoplecounting)
add_subdirectory(libs/librealsense-master)
add_subdirectory(libs/opencv-4.3.0)
set(OpenCV_DIR ${CMAKE_BINARY_DIR}/libs/opencv-4.3.0)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable (CMakeRealSense "CMakeRealSense.cpp" "CMakeRealSense.h")
find_library(realsense2 HINTS libs/librealsense-master)
find_library(opencv HINTS ${CMAKE_BINARY_DIR}/libs/opencv-4.3.0)
target_link_libraries(CMakeRealSense ${OpenCV_LIBS} realsense2)
I have both source files in a libs folder(so directory would look like root/libs/opencv-4.3.0). I am new to cmake so I just might be missing something but its driving me nuts
Some extra notes
1, OpenCV does build the libs when I can build the project, if I remove any include from my self written code it wont make errors which lets me build the project. This does make the opencv librairys which makes me think the linking is the issue
2, If I dont add a project as a executable it does see a opencv include
3, I am doing this to make the project portable, it has to work on a rpi down the line so being able to build it all from source could make it much more portable
EDIT 1
So I did some diging in my files, the exe gets outputted into
root\out\build\x64-Debug\bin but this folder does not only contain the librealsense dll but also the opencv ones. This would make it seem that the exe knows about the library but doesnt want to use it some how? This is very strange to me too
EDIT 2
I managed to find_package(OpenCV) by setting
CMAKE_PREFIX_PATH to win-install which contained a config file, problem now is that iam missing the include dir in said folder
EDIT 3
New error seems to be
Target "opencv_highgui" INTERFACE_INCLUDE_DIRECTORIES property contains
path:
"root/opencv-4.3.0/modules/dnn/include"
which is prefixed in the source directory.
I read somewhere that OpenCV prevents source files being used or something to prevent corruption but now now again at a end when it comes to what iam suppose to be doing
Updated CMake file
project(peoplecounting)
add_subdirectory(librealsense-master)
add_subdirectory(opencv-4.3.0)
set(OpenCV_DIR ${CMAKE_BINARY_DIR})
set(INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_BINARY_DIR}/")
set(BUILD_SHARED_LIBS OFF)
find_package(OpenCV CONFIG REQUIRED)
add_library(opencv INTERFACE)
include_directories(${OpenCV_INCLUDE_DIRS})
#add_executable (CMakeRealSense "CMakeRealSense.cpp" "CMakeRealSense.h")
add_executable(RealSenseTest "TestRealSense.cpp")
find_library(realsense2 HINTS librealsense-master)
message(" cv_libs: " ${OpenCV_INSTALL_PATH})
message(" cv_includes: " ${OPENCV_INCLUDE_DIRS})
target_include_directories(opencv INTERFACE "${OpenCV_INCLUDE_DIRS}")
target_link_libraries(RealSenseTest "${OpenCV_LIBS}" realsense2)```

CMake GLOB not returning any source files?

I am trying to automatically have my Makefiles written for my C++ project using CMake with GLOB. The code and headers are however located in two separate folders.
/Users/username/Coding/Major Projects/ProjectName/Backend
and
/Users/username/Coding/Major Projects/ProjectName/Terminal
The backend has platform independent code. Just a bunch of c and c++ source files. And the Terminal folder has some code that uses the objects specified in Backend to run some tests on them. The reason these are in separate folders is that the Backend code is Multiplatform. So an Xcode project imports it etc. The Terminal folder has the testing code because that is the only one that is trying to compile it into a linux binary.
Anywho, I have the following CMakeList.txt file that I am trying to build to generate the Makefile.
cmake_minimum_required(VERSION 2.8.9)
project(terminalTest)
set(MainSource "/Users/username/Coding/Major Projects/ProjectName/Backend")
set(TerminalSource "/Users/username/Coding/Major Projects/ProjectName/Terminal")
#Bring the headers, such as Student.h into the project
include_directories(${MainSource} ${TerminalSource})
#Can manually add the sources using the set command as follows:
#set(SOURCES src/mainapp.cpp src/Student.cpp)
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "./{${MainSource},${TerminalSource}}/*.cpp")
add_executable(terminalTest ${SOURCES})
And the result of this when I run this from the CMake GUI is a successful configure but an error No SOURCES given to target: terminalTest meaning that my file() command is not working properly.
I think it could have something to do with the fact that I have spaces in my paths but that doesn't seem to do it either. By the way I am putting this file in the Terminal folder and attempting to build from Terminal/Build.
Is there any way to debug and see what sources the GLOB command is bringing in? Can I actually do a multi directory GLOB like this?
Your file(GLOB ...) path looks malformed. You can list the paths to your sources separately in this command to grab all the source files in both directories.
file(GLOB SOURCES
${MainSource}/*.cpp
${TerminalSource}/*.cpp
)

Build c++ Qt CLI Tool with Quazip

so normally I am an embedded software developer first time I need to do something like that... So I am no expert at all at this... I have come this far with a lot of googling and trial and error methods...
What I want to do
I need to write a C++ CLI application which can zip and unzip files.
Quazip
zlib
My general conditions
I want to use Qt and Quazip since Qt doesn't support zipping natively (or does it).
I want to build with Mingw and CMake for Windows 64bit on a Windows 64bit system.
With my knowledge, it would be best if I compile Quazip statically so I don't depend on any other quazip DLL which may be present on other systems...
How I think It works
I need to compile zlib
Then I need to compile Quazip (with zlib)
Link the compiled libraries to my project.
My research
I found two good youtube videos which should help me:
https://www.youtube.com/watch?v=5ZTusMX-Reo
https://www.youtube.com/watch?v=mxlcKmvMK9Q
No of these ways did work out for me.
Also I found that blog post which was a little bit of help...
http://www.antonioborondo.com/2014/10/22/zipping-and-unzipping-files-with-qt/
This site was available till yesterday now it's gone :D
Also I found that old Stackoverflow question which helped me a bit:
How to connect the QuaZip library in CMake
Detailed steps on what I did:
1. Building zlib
Downloaded the latest version of zlib from the official website. version 1.2.11
I build zlib with the following commands:
cd C:/some/Path/zlib-1.2.11
mingw32-make -f win32/Makefile.gcc
That works perfectly I get a
libz.a
libz.dll.a
zlib1.dll
those files and all the header files (11 files) I copy in a seperate folder. Which looks like this:
Build
|
+-include
+-lib
Headers in the include folder the libs in the lib folder.
2. Building Quazip
Now the messy part...
Downloading the newest version of Quazip (version 0.8.1) from the GitHub.
I tried various ways to compile Quazip. The one I stick to was:
Open the .pro file in Qt creator inside the quazip folder (the folder where the sources are located).
So then I added the compiled zlib to the Qmake file.
Following lines I added at the end of the file:
INCLUDEPATH += "C:/Build/include"
LIBS += -L"C:/Build/lib"
LIBS += -lz
Then build Quazip with QtCreator as release Build.
When compiled I get
quazip.dll
libquazip.a
I copy all the header files from quazip (16 fils) in the include folder and the two libs in the lib folder.
Now I have like a zippackage in the build folder.
3. Adding Quazip and zlib to my Project
Copied the FindQuazip.cmake file into my project form the Quazip Repo.
In my CMakeLists.txt I added the following lines:
set(ZLIB_ROOT "C:/BUILD" CACHE PATH "Path to zlib")
find_package(ZLIB REQUIRED)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
find_package(QuaZip REQUIRED)
I need to set the ZLIB_ROOT so CMake uses the zlib I want. If I don't set Root he uses a zlib from a ruby installation from my PC.
Also I added
include_directories(
"${BASE_DIR}/sources"
"${QUAZIP_INCLUDE_DIR}"
)
and at the end
add_executable(${PROJECT_NAME} ${SOURCES} ${INC_ALL} )
target_link_libraries(${PROJECT_NAME} Qt5::Network Qt5::Core Qt5::Widgets
${ZLIB_LIBRARIES}
${QUAZIP_LIBRARIES}
)
Then I needed to add the FindQuazip.cmake since it would find the libs: I edited the following:
FIND_PATH(QUAZIP_LIBRARY_DIR
WIN32_DEBUG_POSTFIX d
NAMES libquazip.a #quazip.dll
HINTS "C:/Build/lib"
PATH_SUFFIXES QuaZip/lib
)
FIND_LIBRARY(QUAZIP_LIBRARIES NAMES libquazip.a HINTS ${QUAZIP_LIBRARY_DIR})
FIND_PATH(QUAZIP_INCLUDE_DIR NAMES quazip.h HINTS ${QUAZIP_LIBRARY_DIR}/../include)
FIND_PATH(QUAZIP_ZLIB_INCLUDE_DIR NAMES zlib.h)
Okay so It took me two days to get to there where I am now.
Now when I run CMake every works fine. He finds the libs.
I can even include a the Header files of Quazip. But when I then try to compile a minimal example the linker can't find any symbols.
Minimal Example:
#include <iostream>
#include <QApplication>
#include "JlCompress.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
cout << __cplusplus << endl;
cout << "Hello World!" << endl;
JlCompress::compressDir("C:/WorkspaceLokal/Test/test.zip", "C:/WorkspaceLokal/Test/TestFolder");
return a.exec();
}
By compiling it I get the following error:
C:\some\path\sources\main.cpp:-1: Fehler: undefined reference to `JlCompress::compressDir(QString, QString, bool)'
This means the linker can't link the function...
My Question
What am I doing wrong?
Do I need to compile Quazip in another way?
Edit 4:
since it now works how to I compile the Quazip as a static lib, so that I can link it statically to my application?
Thanks in advance...
Edit 1:
Here are the QUAZIP variables from Qt creator:
Edit 2:
Okay. Some news. the libquazip.a is something else and doesn't work. If I link against the quazip.dll compiling works. But when I run the application it crashes at the function call compressDir...
Edit 3:
It works. I just needed to copy the quazip.dll to the compiled executable...
From QuaZip's pro file:
# You'll need to define this one manually if using a build system other
# than qmake or using QuaZIP sources directly in your project.
CONFIG(staticlib): DEFINES += QUAZIP_STATIC
This should trigger when you add staticlib to the CONFIG in quazip.pro (2nd line):
CONFIG += qt warn_on staticlib
If you are not using qmake to build Quazip, just make sure that you #define QUAZIP_STATIC in a way specific to your build system

Qt - How to write a FindXXX.cmake file for a certain Qt library

So I really want to start developing my Qt project inside a different IDE (Clion to be exact). I've written my CMake file for the project. It successfully finds Qt Core, Widgets and other libraries but when it comes to including Qt Charts Cmake throws an error:
CMake Error at CMakeLists.txt:44 (find_package):
By not providing "FindQt5Charts.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"Qt5Charts", but CMake did not find one.
This error is successfully removed when I add by hand a "FindQt5Charts.cmake" file in my CLion>bin>Cmake>share>modules (where FindQt.cmake and othere search package files are) but as of now this file is empty.
The Cmake compiles successfully but since it hasn't found the library I'm looking for, my project won't compile. I need your help to write "FindQt5Charts.cmake" file because I'm not 100% sure of its syntax.
NOTE: Qt Charts is installed and its folder could be found in Qt's installation directory. I just need the code for the FindQt5Charts.cmake.
Here's a reference to the FindQt.cmake file
Here is how the beggining of my project's cmake looks:
#Pull needed header files generated by QT first
set(QT_GENERATED qtGenerated)
add_custom_target(${QT_GENERATED})
#Get all header files in the directory
file(GLOB QT_GEN_HEADERS ${QT_BIN}/*.h)
#Copy them to the project dir
foreach (QT_GEN_HEADERS ${QT_GEN_HEADERS})
add_custom_command(TARGET ${QT_GENERATED} PRE_BUILD COMMAND
${CMAKE_COMMAND} -E copy_if_different
${QT_GEN_HEADERS} ${CMAKE_SOURCE_DIR})
endforeach ()
# Find the Qt libraries
foreach (QT_LIBRARIES_REQUIRED ${QT_LIBRARIES_REQUIRED})
find_package(${QT_LIBRARIES_REQUIRED} REQUIRED)
endforeach ()