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.
Related
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; }
I am trying to install AirSim/Unity on MacOS Catalina. When I run Unity/build.sh I get a fatal error:
In file included from /Users/nfirbas/Documents/AirSim/Unity/AirLibWrapper/AirsimWrapper/Source/Logger.cpp:3:
/Users/nfirbas/Documents/AirSim/Unity/AirLibWrapper/AirsimWrapper/Source/Logger.h:6:11: fatal error: 'boost/filesystem.hpp' file not found
#include <boost/filesystem.hpp>
^~~~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
make[2]: *** [CMakeFiles/AirsimWrapper.dir/Source/Logger.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
I have installed boost with brew. It's my first time working with brew so I assume that I need to edit my CMakeList.txt. I tried changing it myself but I didn't get it working.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.5.0)
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(MACOSX TRUE)
endif()
find_path(AIRSIM_ROOT NAMES AirSim.sln PATHS ".." "../.." "../../.." "../../../.." "../../../../.." "../../../../../..")
message(AirSim Root directory: ${AIRSIM_ROOT})
LIST(APPEND CMAKE_MODULE_PATH "${AIRSIM_ROOT}/cmake/cmake-modules")
LIST(APPEND CMAKE_MODULE_PATH "${RPC_SOURCE_DIR}/cmake")
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
INCLUDE("${AIRSIM_ROOT}/cmake/cmake-modules/CommonSetup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/rpc-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/mav-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/airlib-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/airsimwrapper-setup.cmake")
IncludeEigen()
project(AirsimWrapper VERSION 0)
# RPC includes & source files
BuildRpc()
# MavLink source files
BuildMavLink()
#AirLib source files
BuildAirlib()
#AirsimWrapper source files
BuildAirsimWrapper()
###################### Link source files to library ######################################
if (${APPLE})
add_library(
${PROJECT_NAME} MODULE
${RPC_LIBRARY_SOURCE_FILES}
${MAVLINK_LIBRARY_SOURCE_FILES}
${AIRLIB_LIBRARY_SOURCE_FILES}
${AIRSIMWRAPPER_LIBRARY_SOURCE_FILES}
)
set_target_properties(${PROJECT_NAME} PROPERTIES BUNDLE TRUE)
else ()
add_library(
${PROJECT_NAME} SHARED
${RPC_LIBRARY_SOURCE_FILES}
${MAVLINK_LIBRARY_SOURCE_FILES}
${AIRLIB_LIBRARY_SOURCE_FILES}
${AIRSIMWRAPPER_LIBRARY_SOURCE_FILES}
)
endif ()
target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT} -lstdc++ -lpthread -lboost_filesystem)
##################### Build Options #############################3
# Rpc
RpcCheckMSVN()
RpcCmakePkg()
RpcMSVNConfig()
You have told your library target to link against boost_filesystem, but you did not convey in your CMake where to find the Boost header files.
The idiomatic way to find Boost using CMake is to use the configuration files that now ship with Boost (e.g. BoostConfig.cmake), as of Boost version 1.70 and greater. You can make use of these by calling find_package(Boost ...), then linking with the imported target Boost::filesystem:
# Tell CMake to locate Boost on your machine, specifically
# looking for the filesystem library.
find_package(Boost REQUIRED COMPONENTS filesystem)
...
# Link the Boost::filesystem target, which includes the Boost headers.
target_link_libraries(${PROJECT_NAME} PUBLIC
${CMAKE_THREAD_LIBS_INIT}
-lstdc++
-lpthread
Boost::filesystem
)
This will pull in the Boost headers as well, so you don't need an explicit call to target_include_directories() to specify where the Boost headers are.
Note: To ensure the headers are installed on your system, you may need to install boost-devel, in addition to your boost installation.
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.
I am trying to compile a C++ project on linux which utilizes freeglut.
I can not build the project because the libraries are not linked correctly in the CMake files. I researched and tried to apply what was mentioned in a similar answer here: How to compile GLUT + OpenGL project with CMake and Kdevelop in linux?
However build process still fails with the following exception:
/opt/JetBrains/apps/CLion/ch-0/181.4668.70/bin/cmake/bin/cmake --build /home/user/Documents/Projects/GdvProject/cmake-build-debug --target testas -- -j 2
CMake Error at CMakeLists.txt:9 (target_link_libraries):
Cannot specify link libraries for target "GdvProject" which is not built by
this project.
-- Configuring incomplete, errors occurred!
See also "/home/user/Documents/Projects/GdvProject/cmake-build-debug/CMakeFiles/CMakeOutput.log".
make: *** [Makefile:176: cmake_check_build_system] Error 1
My CMakeLists file looks like this:
cmake_minimum_required(VERSION 2.8)
project(GdvProject)
add_executable(testas main.cpp)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} )
target_link_libraries(GdvProject ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
How can I fix this issue?
target_link_libraries wants a target name. Targets are specified by (among others) add_executable, add_library and add_custom_target.
In other words, target_link_libraries(testas ...) should work.
While you're at it, you should consider switching your include_directories(...) to target_include_directories(testas ...) as well.
I tried to import Boost 1.61.0 (downloaded from SourceForge - Boost 1.61.0 as .7z), but failed.
Console:
"D:\Program Files (x86)\JetBrains\CLion 2016.2\bin\cmake\bin\cmake.exe" --build C:\Users\Marczak\.CLion2016.2\system\cmake\generated\WsServer-e351c9f9\e351c9f9\Debug --target WsServer -- -j 4
[ 50%] Linking CXX executable WsServer.exe
CMakeFiles\WsServer.dir\build.make:96: recipe for target 'WsServer.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/WsServer.dir/all' failed
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0':
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [WsServer.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/WsServer.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/WsServer.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/WsServer.dir/rule' failed
mingw32-make.exe: *** [WsServer] Error 2
Makefile:117: recipe for target 'WsServer' failed
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(WsServer)
set(BOOST_ROOT "C:/Users/Marczak/boost_1_61_0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(SOURCE_FILES src/main.cpp)
find_package(Boost)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(WsServer ${SOURCE_FILES})
If I do find_package(Boost 1.61.0 COMPONENTS system filesystem REQUIRED) I get:
Error: Unable to find the requested Boost libraries.
Boost version: 1.61.0
Boost include path: C:/Users/Marczak/boost_1_61_0
Could not find the following static Boost libraries:
boost_system boost_filesystem
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.
I tried to set Boost_USE_STATIC_LIBRARIES on, but it failed too. I use CLion 2016.2.
UPDATE: I tried older versions too. Same error. What's inside the .7z:
In other topics I see lib folder. But here I don't see it. What I should put in BOOST_LIBRARYDIR?
UPDATE 2: Installed binary from https://sourceforge.net/projects/boost/files/boost-binaries/1.61.0/ . I noticed there's new folder: lib64-msvc-14.0. It contains many .dll and .lib files, e.g. boost_atomic-vc140-mt-1_61.dll.
Boost.org says:
If you plan to use your tools from the Windows command prompt, you're in the right place. If you plan to build from the Cygwin bash shell, you're actually running on a POSIX platform and should follow the instructions for getting started on Unix variants. Other command shells, such as MinGW's MSYS, are not supported—they may or may not work.
I'll try using Cygwin.
If you're new to C++, I suggest you to download MinGW distribution maintained by Stephan T. Lavavej (Microsoft C++ developer): https://nuwen.net/mingw.html. It, among other tools and libraries, contains pre-built boost binaries. Unpack it and specify the path to it via Settings | Build, Execution, Deployment | Toolchains.
After that you should be able to compile the program with the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(WsServer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(SOURCE_FILES src/main.cpp)
find_package(Boost REQUIRED COMPONENTS filesystem)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(WsServer ${SOURCE_FILES})
target_link_libraries(WsServer ${Boost_LIBRARIES})
Don't forget to drop CMake cache as find_packages doesn't update successful results due to performance reasons (in CLion it can be done via Cmake toolbar | Cache | red arrows icon).
Some additional remarks:
Boost_USE_STATIC_LIBRARIES is not meant to be set manually, it is set by running find_package(Boost), which uses BOOST_ROOT or BOOST_INCLUDEDIR + BOOST_LIBRARYDIR, you should set those if required. You don't have to do it with the MinGW distro I've linked because it already has boost includes and libraries in accessible locations.
You can check that the paths to libraries are correct by looking at Boost_* variables in CMake cache.
libs directory inside boost sources is unrelated to the problem, it doesn't conitain any binaries
You've downloaded boost binaries built with Visual Studio toolchain, not MinGW, so they are incompatible with your setup. If you don't want to use MinGW package I've linked, you have to either find boost binaries built with correct MinGW version or build it yourself.