I am trying to learn opengl for a class but am having trouble with getting setup. I am using Windows, with the IDE CLion, and cygwin64. So far I have been able to compile and run Gl.h and Glu.h but I am having trouble getting the static library for freeglut.
Right now programs error on the last line in the CMakeLists.txt file:
cmake_minimum_required(VERSION 3.5)
project(OpenGLAugust)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(OpenGLAugust ${SOURCE_FILES})
target_link_libraries(OpenGLAugust libopengl32.a libglu32.a libfreeglut.a)
with error
cannot find -lfreeglut
collect2: error: ld returned 1 exit status
I looked into it and it appears that after downloading freeglut I need to create the library files (specifically the .a file libfreeglut32.a) from the source code. I have tried looking for an explanation on how it is done but haven't found an answer. Would anyone be able to help explain how to do this?
Unlike OpenGL, which availability in a compiler toolchain is mandated by the Windows ABI, FreeGLUT is an optional 3rd party library that must be installed separately.
Most simple course of action would be to simply drop FreeGLUT sources into a subdirectory of your project and just add it to your CMakeLists.txt; FreeGLUT comes with a ready to use CMake configuration, so that will "just work".
On a side note, you should not use
target_link_libraries(OpenGLAugust libopengl32.a libglu32.a …)
but
find_package(OpenGL)
target_link_libraries(OpenGLAugust
${OPENGL_gl_LIBRARY}
${OPENGL_glu_LIBRARY}
… )
I.e. in total you should extract FreeGLUT sources into a subdirectory of your project and change your CMakeLists to
cmake_minimum_required(VERSION 3.5)
project(OpenGLAugust)
find_package(OpenGL)
add_subdirectory(freeglut)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(OpenGLAugust ${SOURCE_FILES})
target_link_libraries(OpenGLAugust
${OPENGL_gl_LIBRARY}
${OPENGL_glu_LIBRARY}
freeglut_static )
Related
I'm new to C++ so be gentle with me. (I know how to code but I get complicated with all the build/compile stuff)
I am using Clion with my C++ project and I have difficult to understand how to include external libraries that I download from the Internet.
for example I have downloaded the OpenCV library (and It located on /usr/local/Cellar/opencv , I cant tell why) and all I have to do in my CmakeLists is to add
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(ArucoMarkersCpp ${OpenCV_LIBS})
this is my full CmakeLists right now:
cmake_minimum_required(VERSION 3.17)
project(ArucoMarkersCpp)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES src/main.cpp)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(ArucoMarkersCpp main.cpp)
target_link_libraries(ArucoMarkersCpp ${OpenCV_LIBS})
I want to include to my project the this library. In all the tutorials I have found, all the examples libraries have inside their main folder, an "include folder" and more. but in this library it seems that it doesn't have it.
The library folder that I downloaded from the link:
I want a General way to do it, without rely on Clion or any other IDE. I want to do it with makefile/Cmakelists (I don't really understand the difference)
Where do people usually put their libraries' files and how they import/include it to their project?
Thanks.
I am trying to compile openGL file including "png.h" header file,
I got the following error:
Open GL version 2.1 ATI-3.2.24
libpng warning: Application built with libpng-1.4.12 but running with 1.6.37
error: png_create_read_struct returned 0.
Failed to read image texture from ../images/ceramic.png
My Cmakelists.txt file :
cmake_minimum_required (VERSION 3.13)
project (teapot)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIR})
add_executable(teapot teapot.cpp)
target_link_libraries(teapot ${OPENGL_gl_LIBRARY} ${GLUT_LIBRARIES} ${PNG_LIBRARIES} )
set(CMAKE_CXX_FLAGS "-I ${CMAKE_OSX_SYSROOT} ${CMAKE_CXX_FLAGS} -std=c++11")
if (APPLE)
set (CMAKE_CXX_FLAGS "-Wno-deprecated-declarations ${CMAKE_CXX_FLAGS}")
endif ()
set_target_properties(teapot PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
cmake . works fine, but when I execute ./teapot the above error occurred. Thanks for any help!
You've probably only have development libraries installed for libpng-1.4.12 and not for libpng-1.6.37 or some other "non-standard" installation.
But the message seems pretty clear you linked against an older version but have a newer version in the runtime path of loadable libraries.
find_package(PNG REQUIRED) is only going to search the "standard" locations via find_library().
You may also want to update your CMakeLists.txt file to use target_link_libraries( ... PNG::PNG). This is simpler than trying to use the PNG variables that get set; which is missing PNG_DEFINITIONS when compiling your project. Refer to the CMake manual buildsystem section about library targets.
You can inspect the variables by using the message() command to print out their values. Some of them will also be stored in CMakeCache.txt.
If libpng isn't in the standard locations then you have to use target_link_libraries( ... /path/to/lib) and target_include_directories( ... /path/to/lib/headers), etcetera to handle it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to get OpenGL to work in CLion, downloaded the bin/include/library files from freeglut and copied them to minGW. They are there, and CLion is using that minGW folder when compiling, however when it tries to compile the test code that freeglut provides, it returns with "fatal error: GL/glut.h: No such file or directory #include <GL/glut.h>"
I have already tried different versions of CMakeLists, all of them return with the same issue. If I copy the GL folder next to the project, I get loads of undefined references.
After trying this CMakeList, I get the same error. (from Running Opengl program with CLion IDE)
cmake_minimum_required(VERSION 3.14)
project(gltest)
set(CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_FLAGS "-lglut")
set (CMAKE_CXX_FLAGS "-lGL")
set (CMAKE_CXX_FLAGS "-lGLU")
include_directories(GL/)
add_executable(gltest Callbacks.c HelloGLUT.c)
target_link_libraries(gltest -lglut -lGL -lGLU)
UPDATE
After using a new CMakeList(as suggested in the comments, im more than grateful to you wonderful people here) I get some new errors, but CLion seems to be able to reach the GL libraries now.
The CMakeList:
cmake_minimum_required(VERSION 3.14)
project(gltest)
set(SOURCE_FILES Callbacks.c HelloGLUT.c)
set(OPENGL_INCLUDE_DIR C:/minGW/mingw64/include)
set(OPENGL_LIBRARY C:/minGW/mingw64/lib)
find_package(OPENGL REQUIRED)
add_executable(gltest ${SOURCE_FILES})
include_directories(${OPENGL_INCLUDE_DIR})
target_link_libraries(gltest ${OPENGL_LIBRARY} )
Errors:
CMakeFiles\gltest.dir/objects.a(Callbacks.c.obj): In function "glutInit_ATEXIT_HACK':
C:/minGW/mingw64/include/GL/freeglut_std.h:637: undefined reference to "__imp___glutInitWithExit"
CMakeFiles\gltest.dir/objects.a(Callbacks.c.obj): In function "glutCreateWindow_ATEXIT_HACK":
And several more undefined references in the same manner to various "__imp___glut" functions. Any ideas?
If I use target_link_libraries(gltest ${OPENGL_LIBRARY} glut GL GLU) I get
"C:/minGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lglut " , same for lGL and lGLU
SOLUTION FOR THOSE WHO RUN INTO THE SAME ERRORS
Make sure you follow the readme and have freeglut installed in a DIFFERENT folder inside mingw (for me it is C:/minGW/mingw64/freeglut) and copy freeglut's include and lib there. Make sure you have a FindFREEGLUT.cmake file (i pulled mine from https://git.omkov.net/Jamozed/Chip8Emulator/releases , if you have a download where it is only the FindFREEGLUT.cmake feel free to post it). Copy the FindFREEGLUT.cmake into your project's directory like "C:/CLionProjects/gltest/cmake/FindFREEGLUT.cmake". After this, I used the following CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(gltest)
set(SOURCE_FILES Callbacks.c HelloGLUT.c)
set(GLUT_INCLUDE_DIR "C:/minGW/mingw64/freeglut/include")
set(OPENGL_LIBRARY_DIR "C:/minGW/mingw64/freeglut/lib")
# GLUT
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIR})
if(NOT GLUT_FOUND)
message(ERROR "GLUT not found!")
endif(NOT GLUT_FOUND)
# OpenGL
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
if(NOT OPENGL_FOUND)
message(ERROR "OPENGL not found!")
endif(NOT OPENGL_FOUND)
add_executable(gltest ${SOURCE_FILES})
target_link_libraries(gltest ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
I have installed opencv, qt, qt creator, cmake on ubuntu 15.10 through VMware on windows.
The opencv is installed in this directory: /home/majidalaeinia/opencv/
The project repository is cloned in this directory: /home/majidalaeinia/Desktop/imgwarp-opencv/
I want to run the project through its CMakeLists.txt in qt creator and when I press Build now on qt creator, I get the following errors:
error: cannot find -lopencv_imgcodecs
error: collect2: error: ld returned 1 exit status
Where is the problem and how can I solve it?
# Majid Alaeinia, from the CMakeLists.txt file you posted it is not specified how CMAKE should find the libraries requested from your project. Also there are no target_link_libraries declared so CMAKE does not know where to link them. Hopefully the following small example template should be helpful for your project:
cmake_minimum_required (VERSION 3.1)
project(yourProject)
find_package( OpenCV REQUIRED )
find_package( Qt5 REQUIRED COMPONENTS Sql )
### this is for c++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
### QT stuff if you want a GUI
set(CMAKE_AUTOMOC ON) # autogenerate qt gui features
set(CMAKE_AUTORCC ON) # used for QT resource Files (if you need)
## Additional operation...
# From here you are specifically linking all OpenCV libraries and executables
### Add executables
add_executable(yourExecutable main/main.cpp ui/res/res.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (yourProject example Qt5::Widgets ${OpenCV_LIBS} Qt5::Sql)
### Add Library
add_library(yourProject_lib SHARED ${SRCS} ${UI_HDRS})
target_link_libraries (yourProject_lib example Qt5::Widgets ${OpenCV_LIBS})
# Majid Alaeinia,I uploaded the repository and went through the code. if you go inside the demo folder and you change the present CMakeLists.txt file with the one I provided below it should compile (It does compile on mine with the provided changes):
project(demo)
cmake_minimum_required(VERSION 2.6)
find_package(Qt5 REQUIRED COMPONENTS Widgets Core)
FIND_PACKAGE( OpenCV REQUIRED )
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/lib ${CMAKE_CURRENT_SOURCE_DIR})
set(demo_SRCS main.cpp projfile.cpp deformwin.cpp myimage.cpp singlephotoview.cpp pointspaint.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#qt5_automoc(${demo_SRCS})
QT5_WRAP_CPP(QOBJ_CPP ${demo_SRCS})
qt5_wrap_ui(helloworld_FORMS_HEADERS deformwin.ui)
add_executable(demo ${demo_SRCS} ${helloworld_FORMS_HEADERS})
target_link_libraries(demo ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} imgwarp-lib opencv_core opencv_imgproc opencv_imgcodecs)
The code in the repository is an old code and still carries Qt4 as main wrappers. I think you probably have Qt5 installed on your computer and in fact the code I provided it will work for Qt5. Use it as a guideline for the other CMakeLists.txt file present inside src folder and change accordingly.
CMake will compile but because it was used Qt4 you need to figure out the most important modules to add, for example the new standard for including QtGui/QApplication is usually substituted by QtWidgets/QApplication
I also wanted to leave my previous answer in case you need a starting point or a initial template. I hope this clarifies a bit more and can get you move forward for your project.
I cannot for the life of me get SDL2 to work with my programming group's project.
I'm using
-Clion 1.2.1
-SDL 2.0.3
-MinGW 5.0
The compiler starts yelling at me from within the Graphics.h file which includes SDL like so:
#include <SDL.h>
#include <SDL_image.h>
with the error being:
fatal error: SDL.h: No such file or directory
compilation terminated.
I tried including with
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
which still yielded the same error.
I downloaded SDL from the MinGW 32/64-bit download of development libraries from: https://www.libsdl.org/download-2.0.php. I also linked the respective ..\SDL2\x86_64-w64-mingw32\include and lib to path in system->advanced system settings->...
Still nothing.
cmake_minimum_required(VERSION 3.3)
project(ClionProjects)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lsdl2")
set(SOURCE_FILES
Kod/Graphics/Graphics.cc
Kod/Graphics/Graphics.h
Kod/Game/Game.cc
Kod/Game/Game.h
Kod/Gameboard/Gameboard.cc
Kod/Gameboard/Gameboard.h
Kod/Meeple/Meeple.cc
Kod/Meeple/Meeple.h
Kod/Player/Player.cc
Kod/Player/Player.h
Kod/Resource/Resource.cc
Kod/Resource/Resource.h
Kod/Tile/Tile.cc
Kod/Tile/Tile.h
Kod/Carcassonne.cc)
add_executable(ClionProjects ${SOURCE_FILES} Kod/Carcassonne.cc)
target_link_libraries(ClionProjects SDL2main SDL2 SDL2_image)
is the cmakefile I have in Clion. I've been bashing my head bloody trying to get this to work and none of the previous Stackoverflow questions managed to solve my issue.
Personally I uses MSYS2 in my project. The MINGW64 vesion of SDL2 and the other library were installed, as well as the other toolchains like gcc, cmake, and pkg-config (toolchains are MINGW64 version as well).
Then I point my CLion toolchain configuration to the c:\msys2\mingw64, given that I install the msys2 at c:\msys2.
The FindPkgConfig CMake module is used to locate the SDL2 library and include files location.
Please find the example below (I also use FreeType2 and harfbuzz as well). The important part is the FindPkgConfig part (pkg_check_modules) and include/link part.
Make sure to reload the configuration after you have modified the cmake file.
cmake_minimum_required (VERSION 2.6)
project (pgengine)
# The version number.
set (Tutorial_VERSION_MAJOR 0)
set (Tutorial_VERSION_MINOR 1)
add_definitions(-std=c++11)
aux_source_directory(src SRC_LIST)
aux_source_directory(src/game SRC_LIST)
aux_source_directory(src/graphics SRC_LIST)
aux_source_directory(src/sound SRC_LIST)
aux_source_directory(src/system SRC_LIST)
aux_source_directory(src/visual_novel SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
INCLUDE(FindPkgConfig)
pkg_check_modules(FREETYPE2 REQUIRED freetype2)
pkg_check_modules(HARFBUZZ REQUIRED harfbuzz)
pkg_check_modules(SDL2 REQUIRED sdl2)
pkg_check_modules(SDL2_IMAGE REQUIRED SDL2_image)
pkg_check_modules(SDL2_MIXER REQUIRED SDL2_mixer)
include_directories(${SDL2_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_MIXER_INCLUDE_DIRS}
${FREETYPE2_INCLUDE_DIRS}
${HARFBUZZ_INCLUDE_DIRS})
link_directories (${SDL2_LIBRARY_DIRS}
${SDL2_IMAGE_LIBRARY_DIRS}
${SDL2_MIXER_LIBRARY_DIRS}
${FREETYPE2_LIBRARY_DIRS}
${HARFBUZZ_LIBRARY_DIRS})
target_link_libraries (pgengine ${SDL2_LIBRARIES}
${SDL2_IMAGE_LIBRARIES}
${SDL2_MIXER_LIBRARIES}
${FREETYPE2_LIBRARIES}
${HARFBUZZ_LIBRARIES})
This has been tested on Windows (MSYS2) and Linux.