C++/CMake can't find libxml++ - c++

I'm a Java/Go/Python dev trying to get my feet wet with the C++ toolchain and having a hard time forcing CMake to find dependencies. I installed libxml++ on Debian using apt-get install libxml++2.6-dev and it installed it to /usr/include/libxml++2.6/libxml++. This is a problem, because that's not the right path relative to /usr/include--if I try to #include <libxml++/libxml++.h> it can't find it obviously, and if I include stuff as e.g. #include <libxml++2.6/libxml++/whatever.h> then whatever.h will be unable to find other header files searching for the libxml++ path, e.g. #include <libxml++/version.h>. There are pkg-config files that come with the library but I couldn't really figure out what to do with them, and using stuff like find_package(LibXml++) or include_directories(${LibXml++_INCLUDE_DIRS}) didn't work.
Looking at the CMake docs there appear to be several ways to solve every simple problem, and I feel like this must be a very simple problem with a simple solution if this is related to the way apt-get installs things. Is there something I can add to force CMake to look specifically in /usr/include/[something]/libxml++ and still import it properly with relative paths, or do I need to just move stuff every time I install it with apt?
Apologies if this is a duplicate, but "libxml++" is an incredibly difficult library to Google, both because of the "++" punctuation and the fact that it's apparently not used anywhere near as much as regular libxml2. I'm happy to read more basic resources but would rather not slog through 30 years of the evolution of C++ to arrive at the answer.
For reference:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(sandbox)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost REQUIRED)
find_package(LibXml2 REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ ${LIBXML2_INCLUDE_DIR})
add_executable(sandbox main.cpp)
target_link_libraries(sandbox ${LIBXML2_LIBRARIES})
Source file:
#include <iostream>
#include <libxml++2.6/libxml++/libxml++.h>
int main() {
std::cout << "hello" << std::endl;
}
Error msg:
[main] Building folder: cpp-sandbox
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /workspaces/cpp-sandbox/build --config Debug --target all -- -j 6
[build] Scanning dependencies of target sandbox
[build] [ 50%] Building CXX object CMakeFiles/sandbox.dir/main.cpp.o
[build] In file included from /workspaces/cpp-sandbox/main.cpp:2:
[build] /usr/include/libxml++-2.6/libxml++/libxml++.h:50:10: fatal error: libxml++/exceptions/internal_error.h: No such file or directory
[build] #include <libxml++/exceptions/internal_error.h>
[build] ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] compilation terminated.
[build] make[2]: *** [CMakeFiles/sandbox.dir/build.make:63: CMakeFiles/sandbox.dir/main.cpp.o] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/sandbox.dir/all] Error 2
[build] make: *** [Makefile:84: all] Error 2
[build] Build finished with exit code 2

LibXml2 and LibXml2++ are different libraries, so find_package() for one of them - LibXml2 - won't help with another.
CMake isn't shipped with "Find" script for LibXml2++, but since you have .pc file for the library, using it is quite simple:
# Search pkg-config utility first
find_package(PkgConfig REQUIRED)
# Then use pkg-config for locate specific package
pkg_check_modules(LIBXMLXX REQUIRED IMPORTED_TARGET libxml++-2.6)
add_executable(sandbox main.cpp)
# Link with the IMPORTED target created by 'pkg_check_modules'
# That target contains both include directories and actual libraries for link.
target_link_libraries(sandbox PkgConfig::LIBXMLXX)
Note, that first parameter for pkg_check_modules call (LIBXMLXX) just denotes the name of the IMPORTED target you want to create. You can specify any name, as you wish.
But the last parameter for pkg_check_modules call (libxml++-2.6) is the name of the package, it should exactly match to the base name of .pc file. Since you have file libxml++-2.6.pc, exactly libxml++-2.6 should be used as the last parameter for pkg_check_modules.
See that question for more details about using pkg-config with CMake.

You may should add this line to your CMakeLists.txt :
include_directories("/usr/include/libxml++2.6/")
( or use any macro to achieve to this result )
then in your main :
#include <iostream>
#include <libxml++/libxml++.h> // <= like this
int main() {
std::cout << "hello" << std::endl;
}
Another way to do :
If it do not fix your problem you can also try to go to the libxml++ page : https://developer.gnome.org/libxml++-tutorial/stable/chapter-introduction.html .
Here you have a link to download libxml++.
Then you just have to follow the instruction in the file call INSTALL, and add the include directory with :
include_directories("<your-path/libxml++2.6>")
and add a new library path for your compilator when it search lib with
link_directories("<your-other-path>/lib")
Hoping it help you

I've managed to solve the apparently same problem (on Ubuntu 20.04 with g++9) by merging the two answers. Some version change may apply
cmake_minimum_required (VERSION 3.10)
project (sandbox)
find_package (LibXml2 REQUIRED)
find_package (PkgConfig REQUIRED)
pkg_check_modules (LIBXMLXX REQUIRED IMPORTED_TARGET libxml++-2.6)
include_directories (${LIBXML2_INCLUDE_DIR})
include_directories ("/usr/include/libxml++2.6/")
add_executable (sandbox main.cpp)
target_link_libraries (sandbox ${LIBXML2_LIBRARIES} PkgConfig::LIBXMLXX)
And with main.cpp source
#include <libxml++/libxml++.h>
int main() { return 0; }

Related

vcpkg wxWidgets CMake linker error with libjpeg.a

I'm trying to use wxWidgets on an arm64 macOS with vcpkg, CMake, and VS Code. Everything is wired up correctly because other vcpkg libraries include, link, and run fine. But, when I try to use wxWidgets there's a linking error.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.22.0)
project(main VERSION 0.1.0)
add_executable(main main.cpp)
set_property(TARGET main PROPERTY CXX_STANDARD 17)
find_package(wxWidgets REQUIRED)
include(${wxWidgets_USE_FILE})
target_include_directories(main PRIVATE ${wxWidgets_INCLUDE_DIRS})
target_link_libraries(main PRIVATE ${wxWidgets_LIBRARIES})
The CMake error I get:
[build] [ 50%] Linking CXX executable main
[build] ld: library not found for -llibjpeg.a>
[build] clang: error: linker command failed with exit code 1 (use -v to see invocation)
The value of the wxWidgets_LIBRARIES list (set by find_package(wxWidgets REQUIRED)):
-L/Users/myname/cpp/vcpkg/packages/wxwidgets_arm64-osx/lib;-pthread;/Users/myname/cpp/vcpkg/packages/wxwidgets_arm64-osx/lib/libwx_osx_cocoau_xrc-3.1.a;/Users/myname/cpp/vcpkg/packages/wxwidgets_arm64-osx/lib/libwx_osx_cocoau_qa-3.1.a;/Users/myname/cpp/vcpkg/packages/wxwidgets_arm64-osx/lib/libwx_baseu_net-3.1.a;/Users/myname/cpp/vcpkg/packages/wxwidgets_arm64-osx/lib/libwx_osx_cocoau_html-3.1.a;/Users/myname/cpp/vcpkg/packages/wxwidgets_arm64-osx/lib/libwx_osx_cocoau_core-3.1.a;/Users/myname/cpp/vcpkg/packages/wxwidgets_arm64-osx/lib/libwx_baseu_xml-3.1.a;/Users/myname/cpp/vcpkg/packages/wxwidgets_arm64-osx/lib/libwx_baseu-3.1.a;-lwx_osx_cocoau_core-3.1;libjpeg.a>;libjpeg.a>;libpng.a>;libpng16d.a>;libz.a>;libz.a>;libtiff.a>;libtiffd.a>;liblzma.a>;liblzma.a>;libjpeg.a>;libjpeg.a>;libz.a>;libz.a>;m;-framework AudioToolbox;-framework WebKit;-lwx_baseu-3.1;libexpat.a>;libexpat.a>;libz.a>;libz.a>;-lwxregexu-3.1;libiconv.tbd;-framework CoreFoundation;-framework Security;-framework Carbon;-framework Cocoa;-framework IOKit;-framework QuartzCore;TIFF::TIFF;expat::expat;ZLIB::ZLIB;png_static
I don't have much experience with CMake, so I don't know what the right angle bracket is for, but is that the problem? Could its being the first non-full-path file in the list mean that it doesn't know where to look?
-L is for directories, and -l is for individual library files. I see you have mixed .a files with directories. You'll need to fix that.
Your best bet is to debug cmake configure with --trace-expand and see who is setting wxWidgets_LIBRARIES to a incomplete and very strange generator expression libjpeg.a>;libjpeg.a>;libpng.a>;libpng16d.a>;
Another suspicious thing is that your library paths contain packages/wxwidgets_arm64-osx which indicates either wrong usage of vcpkg or there is a -config.cmake involved which was not fixed by vcpkg. (everything vcpkg finds via cmake should be living in /installed/<triplet>)

Issue linking c++ library with cmake

I am trying to link the following library : nngpp
using the following commands
mkdir build
cd build
cmake ..
make
make install
However when testing the demos or using the library in a project with the following in CMakeLists.txt :
...
add_executable(target main.cpp)
target_link_libraries(target nngpp)
I get the following error:
fatal error: 'nngpp/nngpp.h' file not found
#include <nngpp/nngpp.h>
^~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/rest.dir/rest/server.o] Error 1
make[1]: *** [CMakeFiles/rest.dir/all] Error 2
make: *** [all] Error 2
note : The library is header-only. but I don't want to copy it in my project.
With
find_package(nngpp)
using the library is straightforward:
add_executable(target main.cpp)
target_link_libraries(target nng::nngpp)
Here nng::nngpp is IMPORTED library, so it cares about include directories for you.
It's hard to know exactly what's wrong without more information. I would suggest running make VERBOSE=1. This will show you the command line that is being executed when trying to compile the file that gives you the error.
Look in the command line for include flags (e.g. -I flags if you're using gcc). Find the directory for nngpp and double-check if your header files are in there, and what the correct path relative to that directory is to reference the header file. Maybe you need to only #include <nngpp.h> instead?
The following worked :
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(comm)
set(CMAKE_CXX_STANDARD 11)
find_package(nng REQUIRED)
find_package(Threads)
find_package(nngpp REQUIRED)
add_executable(server src/tfo-server.cpp)
target_link_libraries(server nng::nng nng::nngpp)
This also worked:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(comm)
set(CMAKE_CXX_STANDARD 11)
add_executable(server src/tfo-server.cpp)
include_directories(server /usr/local/include)

fatal error: allegro5\allegro.h: No such file or directory yet found on cmake

So I did a game on Visual Studio long ago. 2 years ago I uploaded the sources to github NEGU93/ForbiddenDesert just to have it there. Now I changed from windows to linux and I wanted to go back and compile this game for linux.
I remember I used allegro for the GUI so I installed it following this steps Installing Allegro 5. When I coded the game I remember downloading an allegro prepared for Visual Studio directly so I didn't have any problems.
I created a CMakeLists.txt (never did that before, so I am quite new in that area):
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 2.8)
# projects name
project(ForbiddenDesert)
set(CMAKE_CXX_STANDARD 11) # enable C++11 standard
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
# Finds Allegro using pkgconfig, so it must be configured correctly
find_package(Allegro5 REQUIRED)
# Set include and lib dirs.
include_directories(${ALLEGRO_INCLUDE_DIR})
set(FD_LIBS ${LIBS} ${OBJC_LIBRARIES} ${ALLEGRO_LIBRARIES})
# The following folder will be included
# include_directories(${PROJECT_SOURCE_DIR}/src)
# include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories("${PROJECT_SOURCE_DIR}")
file(GLOB all_SRCS
"*.h"
"*.cpp"
)
add_executable(forbidden ${all_SRCS})
target_link_libraries(forbidden ${FD_LIBS})
I am using a file named FindAllegro5.cmake inside a folder named cmake/. The file is a copy of eruta/FindAllegro5.cmake.
When running cmake . I get the following:
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'allegro-5'
-- Found allegro-5, version 5.2.4
-- Found Allegro5: /usr/lib/liballegro.so;/usr/lib/liballegro_image.so;/usr/lib/liballegro_font.so;/usr/lib/liballegro_primitives.so;/usr/lib/liballegro_ttf.so;/usr/lib/liballegro_audio.so;/usr/lib/liballegro_dialog.so;/usr/lib/liballegro_memfile.so;/usr/lib/liballegro_acodec.so;/usr/lib/liballegro_color.so;/usr/lib/liballegro_main.so;/usr/lib/liballegro_physfs.so
So far so good. Yet, when I run make
In file included from /home/ubuntu/Documents/GitHub/ForbiddenDesert/Button.h:5:0,
from /home/ubuntu/Documents/GitHub/ForbiddenDesert/ArcheologistButton.h:4,
from /home/ubuntu/Documents/GitHub/ForbiddenDesert/ArcheologistButton.cpp:1:
/home/ubuntu/Documents/GitHub/ForbiddenDesert/allegro.h:5:10: fatal error: allegro5\allegro.h: No such file or directory
#include <allegro5\allegro.h>
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/forbidden.dir/build.make:62: recipe for target 'CMakeFiles/forbidden.dir/ArcheologistButton.cpp.o' failed
make[2]: *** [CMakeFiles/forbidden.dir/ArcheologistButton.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/forbidden.dir/all' failed
make[1]: *** [CMakeFiles/forbidden.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I've seen that the FindAllegro5.cmake has:
# Include dir
find_path(Allegro5_INCLUDE_DIR
NAMES allegro5/allegro5.h
PATHS ${Allegro5_PKGCONF_INCLUDE_DIRS}
)
So I changed the include to #include <allegro5/allegro5.h> but still no solution.
I haven't been able to find the solution (I've found plenty of information about the "fatal error: allegro5\allegro.h: No such file or directory" but haven't found one that applies to my case).
So I installed allegro with the following: Quickstart Allegro and then I created the hello.c file and compiled it as it says there (gcc hello.c -o hello $(pkg-config allegro-5 allegro_font-5 --libs --cflags)) and it worked. So the problem will be how to add those flags to the cmakelists.txt
So I tried:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $(pkg-config allegro-5 allegro_font-5 allegro_primitives-5 allegro_ttf-5 allegro_image-5 allegro_audio-5 allegro_acodec-5 --libs --cflags)")
But still didn't work.
Running find /usr/ -name "allegro*"
I found the header allegro5.h on /usr/include/allegro5/allegro5.h and some other files on /usr/lib/pkgconfig/.
So I did:
INCLUDE_DIRECTORIES( /usr/include/allegro5 )
LINK_DIRECTORIES( /usr/lib/pkgconfig )
TARGET_LINK_LIBRARIES(game liballegro.a )
Still not working.
I tried adding the libraries directly from the build I compiled from the source code on github (following the info I found here Allegro and CMake) and still didn't work:
#Include Allegro
include_directories(/home/ubuntu/Documents/GitHub/allegro5/build/include)
include_directories(/home/ubuntu/Documents/GitHub/allegro5/build/lib/Headers)
link_directories(/home/ubuntu/Documents/GitHub/allegro5/build/lib)
#connect all the libraries you need
set(game_LIBS liballegro.so liballegro_dialog.so liballegro_image.so)
target_link_libraries(game ${game_LIBS})
Under Ubuntu I would just install allegro with apt-get:
apt-get install liballegro5-dev
Then the includes would be at the right place (/usr/include/allegro5/...)
You say you installed allegro, but I'm not too sure whether it would be the same as from a package. If it was from a build, it is often that the default installation directory is /usr/local instead of /usr. So the includes would be in /usr/local/include/allegro5/...
That being said, your include() command in cmake is correct:
include_directories(${ALLEGRO_INCLUDE_DIR})
However, your libraries handling is completely wrong. You never have to change LD_FLAGS. (At least, in the last 6 or 7 years I've used cmake, I never had a need.) Instead, you want to use the target_link_libraries(). Something like this:
target_link_libraries(${PROJECT_NAME} ${ALLEGRO_LIBRARIES})
It will work within (as in after) a project(...).
If you want to verify that the variables are correctly named, you may use the message() command. Some libraries use names such as NAME_LIBRARY or NAME_INCLUDE_PATH. They should follow the proper naming convention, but nothing forces them to, so you often find some funkiness there.
The following will print a message out:
message("allegro libraries = " ${ALLEGRO_LIBRARIES})
If the variable name is not correct (or the library was not found), you will see nothing. If you got it right, it will show you the variable libraries. I would imagine that is right, but the LD_FLAGS was probably what you got wrong.

Include external libraries into CLion project through CMake

I struggle to get GLFW Windows pre-compiled binaries working within my CLion Project. Those libraries are placed in a external directory. I do not want them to be in my project library but should (of course) be shipped when releasing the application. I am new to C++ but I thought to accomplish this might be as easy as it is in Java (Intellij Idea -> dependencies -> ...).
GLFW Windows pre-compiled binaries
I use MinGW 5.0 and CMake 3.10.2;
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(Hatsudouki_core)
set(CMAKE_CXX_STANDARD 17)
link_directories(F:\\C++\\ExternalLibraries\\GLFW\\lib-mingw-w64)
include_directories(F:\\C++\\ExternalLibraries\\GLFW\\include)
add_executable(Hatsudouki_core main.cpp)
target_link_libraries(Hatsudouki_core glfw3)
Main.cpp
#include <iostream>
#include <GLFW/glfw3.h>
int main() {
if (!glfwInit())
std::cout << "error!" << std::endl;
else
std::cout << "success!" << std::endl;
return 0;
}
Build output
"F:\C++\CLion 2018.1\bin\cmake\bin\cmake.exe" --build C:\Users\simon\CLionProjects\Hatsudouki-core\cmake-build-debug --target Hatsudouki_core -- -j 4
[ 50%] Linking CXX executable Hatsudouki_core.exe
CMakeFiles\Hatsudouki_core.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/simon/CLionProjects/Hatsudouki-core/main.cpp:5: undefined reference to `glfwInit'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Hatsudouki_core.exe] Error 1
CMakeFiles\Hatsudouki_core.dir\build.make:96: recipe for target 'Hatsudouki_core.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Hatsudouki_core.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/Hatsudouki_core.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Hatsudouki_core.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/Hatsudouki_core.dir/rule] Error 2
Makefile:117: recipe for target 'Hatsudouki_core' failed
mingw32-make.exe: *** [Hatsudouki_core] Error 2
I tried following solutions mentioned here:
- GLFW doc and GLFW doc2 (find package does not work, no CMake file)
- Github issue report related to Github issue report 2 which then leads to the solution to put FindGLFW.cmake into some directory? Tried to put it here GLFW\FindGLFW.cmake but does not work- Linking did not work as well as mentioned here: Stackoverflow
Image GLFW directory: GLFW Windows pre-compiled binaries
I think I just do not understand how CMake, external Libraries and C++ work together to accomplish this fairly easy task. I believe comparison to Java would help (used to work with gradle)
EDIT 1
As suggested I added following:
I put the Findglfw3.cmake into PROJECT/cmake/Modules/:
# Copyright (c) 2015 Andrew Kelley
# This file is MIT licensed.
# See http://opensource.org/licenses/MIT
# GLFW_FOUND
# GLFW_INCLUDE_DIR
# GLFW_LIBRARY
find_path(GLFW_INCLUDE_DIR NAMES F:\\C++\\ExternalLibraries\\GLFW\\include\\GLFW\\glfw3.h)
find_library(GLFW_LIBRARY NAMES glfw glfw3)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW DEFAULT_MSG GLFW_LIBRARY GLFW_INCLUDE_DIR)
mark_as_advanced(GLFW_INCLUDE_DIR GLFW_LIBRARY)
And added following lines into my CMakeLists.txt:
find_package(glfw3 REQUIRED)
include_directories(${glfw3_INCLUDE_DIRS})
set(LIBS ${LIBS} ${glfw3_LIBRARIES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
target_link_libraries(hatsudouki_core ${LIBS})
I also tried in the Findglfw3.cmake:
find_path(GLFW_INCLUDE_DIR NAMES GLFW/glfw3.h)
which is the same in the original file. Both do not work; error:
"F:\C++\CLion 2018.1\bin\cmake\bin\cmake.exe" --build C:\Users\simon\CLionProjects\Hatsudouki-core\cmake-build-debug --target Hatsudouki_core -- -j 4
CMake Error at CMakeLists.txt:6 (find_package):
-- Configuring incomplete, errors occurred!
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
See also "C:/Users/simon/CLionProjects/Hatsudouki-core/cmake-build-debug/CMakeFiles/CMakeOutput.log".
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.
Makefile:175: recipe for target 'cmake_check_build_system' failed
Could not find a package configuration file provided by "glfw3" with any of
the following names:
glfw3Config.cmake
glfw3-config.cmake
Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files. If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.
mingw32-make.exe: *** [cmake_check_build_system] Error 1
As explained here
Make Findglfw3.cmake file in PROJECT/cmake/Modules/ which looks like
# GLFW_FOUND
# GLFW_INCLUDE_DIR
# GLFW_LIBRARY
set(FIND_GLFW_PATHS "F:\\C++\\ExternalLibraries\\GLFW")
find_path(GLFW_INCLUDE_DIR NAMES GLFW/glfw3 GLFW/glfw3.h PATH_SUFFIXES include PATHS ${FIND_GLFW_PATHS})
find_library(GLFW_LIBRARY NAMES glfw3 glfw3.a libglfw3 libglfw3.a PATH_SUFFIXES lib-mingw PATHS ${FIND_GLFW_PATHS})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW DEFAULT_MSG GLFW_LIBRARY GLFW_INCLUDE_DIR)
mark_as_advanced(GLFW_INCLUDE_DIR GLFW_LIBRARY)
Define Module Path in CMakeLists.txt
#Define module path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules")
Also Link include directory and library with project in CMakeLists.txt
#Define static GLFW libraries and header files
find_package(glfw3 REQUIRED)
include_directories(${GLFW_INCLUDE_DIR})
...
target_link_libraries(Hatsudouki_core ${GLFW_LIBRARY})
At first you need to install glfw package. If you are using MSYS2 then it can be installed using pacman -S mingw-w64-x86_64-glfw on 64-bit windows and pacman -S mingw-w64-i686-glfw on 32-bit windows. And you need to use find_package to let the cmake locate the glfw library automatically instead of manually locating them. If you are not using MSYS2 then it might be little difficult. Check out this code.
cmake_minimum_required(VERSION 3.10)
project(Hatsudouki_core)
set(CMAKE_CXX_STANDARD 17)
add_executable(Hatsudouki_core main.cpp)
find_package(glfw3 REQUIRED) # Find the GLFW library
target_link_libraries(Hatsudouki_core PRIVATE glfw) # Finally, link to them.
I suggest you to use MSYS2 If you need pre-compiled libraries. It has a very good package manager called as pacman.

Cmake target_link_libraries not linking my library

I'll begin stating that I'm almost complete dumb in Cmake matter.
I have the following CMakeLists.txt for a Kdevelop 4.1 project:
project(uart)
find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include_directories( ${KDE4_INCLUDES} ${QT_INCLUDES} src/include src/include/QSerialDevce )
add_subdirectory(doc)
add_subdirectory(src)
add_subdirectory(icons)
link_directories(/usr/lib)
find_library(SERIALDEVICE_LIB qserialdeviced)
add_executable(uart ${uart_SRCS})
target_link_libraries(uart ${SERIALDEVICE_LIB})
When I try to build my project I see:
uart/build> make -j2
-- Found Qt-Version 4.6.3 (using /usr/bin/qmake-qt4)
-- Found X11: /usr/lib64/libX11.so
-- Found KDE 4.5 include dir: /usr/include/kde4
-- Found KDE 4.5 library dir: /usr/lib64/kde4/devel
-- Found the KDE4 kconfig_compiler4 preprocessor: /usr/bin/kconfig_compiler4
-- Found automoc4: /usr/bin/automoc4
CMake Error at CMakeLists.txt:16 (add_executable):
add_executable called with incorrect number of arguments
CMake Error: Attempt to add link library "/usr/lib/libqserialdeviced.so" to target "uart" which is not built by this project.
-- Configuring incomplete, errors occurred!
make: *** [cmake_check_build_system] Error 1
*** Failed ***
Everything I read says that add_executable and target_link_libraries should look like the last two lines of my file:
add_executable(uart ${uart_SRCS})
target_link_libraries(uart ${SERIALDEVICE_LIB})
If I change those two lines of CMakeLists.txt leaving it as:
project(uart)
find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include_directories( ${KDE4_INCLUDES} ${QT_INCLUDES} src/include src/include/QSerialDevce )
add_subdirectory(doc)
add_subdirectory(src)
add_subdirectory(icons)
link_directories(/usr/lib)
find_library(SERIALDEVICE_LIB qserialdeviced)
target_link_libraries(${SERIALDEVICE_LIB})
I see:
uart/build> make -j2
-- Found Qt-Version 4.6.3 (using /usr/bin/qmake-qt4)
-- Found X11: /usr/lib64/libX11.so
-- Found KDE 4.5 include dir: /usr/include/kde4
-- Found KDE 4.5 library dir: /usr/lib64/kde4/devel
-- Found the KDE4 kconfig_compiler4 preprocessor: /usr/bin/kconfig_compiler4
-- Found automoc4: /usr/bin/automoc4
-- Configuring done
-- Generating done
-- Build files have been written to: uart/build
[ 11%] Built target doc-handbook
[ 11%] Built target uart_automoc
Linking CXX executable uart
CMakeFiles/uart.dir/uart.o: In function `uart::setupSerial()':
uart/src/uart.cpp:126: undefined reference to `AbstractSerial::AbstractSerial(QObject*)'
CMakeFiles/uart.dir/uart.o: In function `uart::setupEnumerator()':
uart/src/uart.cpp:108: undefined reference to `SerialDeviceEnumerator::SerialDeviceEnumerator(QObject*)'
CMakeFiles/uart.dir/uart.o: In function `uart::setupSerial()':
uart_/uart/src/uart.cpp:136: undefined reference to `AbstractSerial::enableEmitStatus(bool)'
CMakeFiles/uart.dir/uart.o: In function `uart::setupEnumerator()':
uart_/uart/src/uart.cpp:112: undefined reference to `SerialDeviceEnumerator::setEnabled(bool)'
collect2: ld returned 1 exit status
make[2]: *** [src/uart] Error 1
make[1]: *** [src/CMakeFiles/uart.dir/all] Error 2
make: *** [all] Error 2
*** Failed ***
That clearly shows that target_link_libraries is not linking my qserialdeviced.
qserialdeviced is at /usr/lib/libqserialdeviced.so.1.0.0, correctly simlinked to /usr/lib/libqserialdeviced.so and easily found if I manually add it in the Makefile.
I obviously tried:
target_link_libraries(-lqserialdeviced)
with no change.
I also tried:
if ("${SERIALDEVICE_LIB}" STREQUAL "SERIALDEVICE_LIB-NOTFOUND")
message(FATAL_ERROR "'qserialdeviced' wasn't found!")
else()
message("'qserialdeviced' found: " ${SERIALDEVICE_LIB})
endif ()
But this test succeeds. The library is found:
'qserialdeviced' found: /usr/lib/libqserialdeviced.so
Can anybody please help me to understand what happens here?
I am using Linux Fedora 13, cmake version 2.8.0, gcc (GCC) 4.4.5 20101112 (Red Hat 4.4.5-2) and kdevelop-4.1.0-1.fc13.x86_64.
Thanks i advance.
EDIT:
As suggested by #DatChu, I split my CMakeLists.txt across my subdirectories and everything makes sense to me now.
Thanks everbody!
For the original CMakeLists.txt file, the problem is not with target_link_libraries but with add_executable
add_executable(uart ${uart_SRCS})
where did you set your uart_SRCS variable? Do you have
set(uart_SRCS src/blahblah.cpp src/somethingblahblah.cpp)
I think you might misunderstand what add_subdirectory does. It does not add the source files inside. It tells CMake to descend into that folder and look for another CMakeLists.txt. You typically use it when you have a sub-project inside of your project folder.
If you have many source files which you don't want to manually set, you can also do
file(GLOB uart_SRCS src/*.cpp src/*.c)
The downside is you need to manually re-run CMake in order for it to detect new files. See Jack's comment on why this might not be what you want to use.
Your CMakeLists.txt will most likely be
project(uart)
find_package(Qt4 REQUIRED)
include (${QT_USE_FILE})
find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include_directories( ${KDE4_INCLUDES} ${QT_INCLUDES} src/include src/include/QSerialDevice )
link_directories(/usr/lib)
file(GLOB uart_SRCS src/*.cpp src/*.h)
file(GLOB uart_HDRS include/*.h include/QSerialDevice/*.h)
find_library(SERIALDEVICE_LIB qserialdeviced)
add_executable(uart ${uart_SRCS} ${uart_HDRS})
target_link_libraries(uart ${SERIALDEVICE_LIB} ${QT_LIBRARIES})
This isn't really a direct solution, but I was having such difficulty with "undefined reference" errors (solved previously by linking the appropriate libraries, but not in this case), until I just discovered something - an incompatibility with c vs cpp somehow. The files that defined these reference functions were in .c files (which would default cmake to compile with a C compiler.) and my file referencing these functions is a .cpp file (using g++ compiler or whatever your environment c++ compiler is). Once I changed the .c file to .cpp the "undefined reference" errors disappeared. Above it looks like your uart file is .cpp, but maybe check what the other files are and try this method. It's probably not the appropriate solution or even one at all, but this might get you through the day and moving forward.