CLion compile glut linux - c++

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.

Related

CMake can not find SFML lib

I have a project depends on SFML lib on C++. I trying to make it with CMake.
CMakeLists.txt is:
cmake_minimum_required(VERSION 3.16.3)
project(3D_Renderer_from_scratch)
set(CMAKE_CXX_STANDARD 17)
include_directories(headers source)
set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML COMPONENTS window graphics system)
set(SOURCES
Main.cpp
source/Application.cpp
source/Box.cpp
source/Camera.cpp
source/FileReader.cpp
source/KeyboardHandler.cpp
source/Sphere.cpp
source/Triangle.cpp
source/Window.cpp
source/World.cpp
)
add_executable(executable ${SOURCES})
target_link_libraries(executable ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
After running cmake . I have the following error:
$ cmake .
-- Requested SFML configuration (Static) was not found
CMake Warning at CMakeLists.txt:10 (find_package):
Found package configuration file:
/usr/lib/x86_64-linux-gnu/cmake/SFML/SFMLConfig.cmake
but it set SFML_FOUND to FALSE so package "SFML" is considered to be NOT
FOUND.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mcjohn974/3D_Renderer_from_scratch
How can I fix it ? (sfml lib is already installed)
As already mentioned in the comments (why not write it as an answer?), CMake was looking for static SFML libraries and it most likely just found shared libraries.
Either make sure that static libraries (the ones with the -s suffix) are provided.
Or don't request static libraries, which you can achieve be not setting SFML_STATIC_LIBRARIES.

CMake Error: Please set them or make sure they are set and tested correctly in the CMake files: QUICKFIX_LIBRARY linked by target "run" in directory

I try to build a project on CLion with Cygwin and I got this error :
-- Found Boost: /usr/include (found version "1.66.0") found components: system thread regex iostreams chrono date_time atomic
-- Configuring done
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
QUICKFIX_LIBRARY
linked by target "run_engine" in directory /cygdrive/c/Users/33671/Desktop/main_program
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
I try to generate the .exe fail but nothing appeared because of this error.
Any idea ?
So I changed the Path of QUICKFIX_LIBRARY in the CmakeCache.txt and then I don't have the error. But in reality it's still not finding it because once it's written in CMakeCache.txt, it doesn't check anymore if it's really include or not.
My lib is located like this : project_name/exchanges/quickfix/lib/cygquickfix-16.dll.
The lib is cygquickfix-16.dll.
I added a control message to see if he found or not the library.
I still have an ERROR at the end.
cmake_minimum_required(VERSION 3.5)
project(test)
set(CMAKE_CXX_STANDARD 14)
SET(Boost_USE_STATIC_LIBS OFF)
SET(Boost_USE_MULTITHREAD ON)
SET(CMAKE_PREFIX_PATH /lib/)
FIND_PACKAGE(Boost REQUIRED COMPONENTS system thread regex iostreams)# 1.66.0
IF(Boost_FOUND)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
ENDIF(Boost_FOUND)
SET(USED_LIBS ${Boost_SYSTEM_LIBRARY} ${Boost_THREAD_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_IOSTREAMS_LIBRARY})
add_executable(run_engine main_poco.cpp ....)
include_directories(.)
include_directories(./exchanges/quickfix/include)
find_library(QUICKFIX_LIBRARY quickfix HINTS ./exchanges/quickfix/lib)
message("-- QUICKFIX_LIBRARY: ${QUICKFIX_LIBRARY}")
target_link_libraries(run_engine ${QUICKFIX_LIBRARY} -lPocoDataMySql -l...)
And then I get this in CMake Debug :
C:\Users\33671\AppData\Local\JetBrains\CLion2022.1\cygwin_cmake\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=/usr/bin/make.exe -DCMAKE_C_COMPILER=/usr/bin/gcc.exe -DCMAKE_CXX_COMPILER=/usr/bin/c++.exe -G "CodeBlocks - Unix Makefiles" -S /cygdrive/c/Users/33671/Desktop/project -B /cygdrive/c/Users/33671/Desktop/project/cmake-build-debug
-- QUICKFIX_LIBRARY: QUICKFIX_LIBRARY-NOTFOUND
-- Configuring done
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
QUICKFIX_LIBRARY
linked by target "run_engine" in directory /cygdrive/c/Users/33671/Desktop/project
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.

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>)

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.

Clion mongodb dependency setup

I am getting the following error:
--Configuring incomplete, errors occurred!
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
See also "C:/Users/GyuriX/.CLion2016.1/system/cmake/generated/Projects-33418280/33418280/Debug/CMakeFiles/CMakeOutput.log".
Please set them or make sure they are set and tested correctly in the CMake files:
BSON_LIBRARY
linked by target "Projects" in directory D:/DEV/C/Projects
MONGODB_LIBRARY
linked by target "Projects" in directory D:/DEV/C/Projects
Makefile:443: recipe for target 'cmake_check_build_system' failed
mingw32-make.exe: *** [cmake_check_build_system] Error 1
When I try to compile just a simple Hello World program using the following CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(Projects)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
include_directories(C:/mongo-c-driver/lib)
find_library(MONGODB_LIBRARY mongoc-1.0)
find_library(BSON_LIBRARY bson-1.0)
add_executable(Projects ${SOURCE_FILES})
target_link_libraries(Projects ${MONGODB_LIBRARY} ${BSON_LIBRARY})
I have compiled the required libraries based on what the mongodb wiki said, so I have the library files:
The files I have compiled
So the question is that what am I doing wrong and what should I do to be able to use the mongodb in CLion.
Error message means that find_library fails to find requested libraries.
As you install libraries into non-default directories (according to the screenshot, it is C:\mongo-c-driver\bin), you need to hint for find_library() where it should search.
One possible way is to set CMake variable CMAKE_LIBRARY_PATH:
set(CMAKE_LIBRARY_PATH "C:\\mongo-c-driver\\bin")
# Now find_library should be able to find libraries
find_library(MONGODB_LIBRARY mongoc-1.0)
find_library(BSON_LIBRARY bson-1.0)
There are others way for hint to find_library, see its documentation about search algorithm it uses.