hnllei 2020-06-01
{
OpenGL安装
安装命令如下:
$ sudo apt install build-essential
$ sudo apt install libgl1-mesa-dev
$ sudo apt install libglu1-mesa-dev
$ sudo apt install freeglut3 freeglut3-dev
1
2
3
4
cmake编译OpenGL程序
project
├── MeshLib
│ └── core
│ └── bmp
│ └── RgbImage.cpp
├── MyDemo
│ ├── main.cpp
│ ├── CMakeLists.txt
│ └── build
│
1
2
3
4
5
6
7
8
9
10
其他头文件的路径直接在.cpp里体现出来,如:
在main.cpp中:
#include <GL/glut.h>
#include "../MeshLib/core/viewer/Arcball.h"
#include "../MeshLib/core/bmp/RgbImage.h"
#include "../MeshLib/core/Geometry/quat.h"
1
2
3
4
否则,如果直接写 #include “Arcball.h” 之类的,就需要在CMakeLists.txt中包含所有头文件路径(如glut.h就需要指定路径 OpenGL_INCLUDE_DIR)
若使用freeglut的话
MyDemo/CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
add_compile_options(-std=c++11)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/build/CMakeModules/")
project(MyDemo)
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIR})
find_package(FREEGLUT REQUIRED)
include_directories(${FREEGLUT_INCLUDE_DIR})
add_executable(${PROJECT_NAME} ${PROJECT_BINARY_DIR}/../../MeshLib/core/bmp/RgbImage.cpp main.cpp )
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARY} ${FREEGLUT_LIBRARY} ${OPENGL_LIBRARY})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
但是光这样还是没办法的,因为找不到freeglut库,这时候需要再写一个搜索package frerglut的CMakeLitsts.txt
在build目录下mkdir一个CMakeModules, 新建一个文件名FindFREEGLUT.cmake的文件,编辑内容如下(为什么是在build目录下新建而不是在MyDemo目录下?因为我们是进入build中,执行命令”cmake ..“的)
# Try to find the FREEGLUT library
#
# FREEGLUT_INCLUDE_DIR
# FREEGLUT_LIBRARY
# FREEGLUT_FOUND
FIND_PATH(
FREEGLUT_INCLUDE_DIR GL/freeglut.h GL/gl.h GL/glu.h GL/glew.h
${CMAKE_INCLUDE_PATH}
$ENV{include}
${OPENGL_INCLUDE_DIR}
/usr/include
/usr/local/include
)
SET(STORE_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
SET(CMAKE_FIND_FRAMEWORK NEVER)
FIND_LIBRARY(
FREEGLUT_LIBRARY
NAMES freeglut_static freeglut glut GL
PATH
/opt/local/lib
${CMAKE_LIBRARY_PATH}
$ENV{lib}
/usr/lib
/usr/local/lib
)
SET(CMAKE_FIND_FRAMEWORK ${STORE_CMAKE_FIND_FRAMEWORK})
IF (FREEGLUT_INCLUDE_DIR AND FREEGLUT_LIBRARY)
SET(FREEGLUT_FOUND TRUE)
ENDIF (FREEGLUT_INCLUDE_DIR AND FREEGLUT_LIBRARY)
IF (FREEGLUT_FOUND)
IF (NOT FREEGLUT_FIND_QUIETLY)
MESSAGE(STATUS "Found FREEGLUT: ${FREEGLUT_LIBRARY}")
ENDIF (NOT FREEGLUT_FIND_QUIETLY)
ELSE (FREEGLUT_FOUND)
IF (FREEGLUT_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find FREEGLUT")
ENDIF (FREEGLUT_FIND_REQUIRED)
ENDIF (FREEGLUT_FOUND)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
若使用glut的话
则则比较简单:
cmake_minimum_required (VERSION 2.6)
project(OpenGL)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIR})
set(CMAKE_CXX_FLAGS "-g -Wall")
add_executable(OpenGL Main.cpp)
target_link_libraries(OpenGL ${GLUT_LIBRARY} ${OPENGL_LIBRARY})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
可能出现的错误
if you use `-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated
解决方法 add_compile_options(-fpermissive -std=c++11)
undefined reference to symbol …
这类错误通常是链接.so, .a文件失败产生的,但是都会给出具体是哪个lib没有:”/lib/x86_64-linux-gnu/libz.so: error adding symbols: DSO missing from command line”
解决方法,
1.用各种方法把libz.so放到指定目录下(方法太多了自行查询)
2.若目录下已有libz.so,则在target_link_libraries中加上该libz.so( ${PROJECT_SOURCE_DIR}//libz.so)
————————————————
版权声明:本文为CSDN博主「SarKerson」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sarkerson/java/article/details/78068417
}