c++ - 在 Linux 上动态和静态地使用 C++ 库
问题描述
如果这是一个重复的问题,我深表歉意,但我无法在任何地方找到答案。
我是 C++ 新手,希望开始学习 OpenGL。为此,我需要同时设置GLEW和GLFW。尽管阅读了文档并进行了大量研究,但我无法弄清楚如何静态和动态地利用 C++ 库。我找不到答案的主要原因是因为我在 Ubuntu 上运行,而大多数资源都是在 Windows 上运行的。
我已尝试使用CMake构建库,遵循文档。我似乎成功地构建了这些库,但是当将这些库与编译器一起使用时,问题就出现了,我再次找不到足够好的答案。
我尝试了以下步骤来安装 GLEW 和 GLFW:
- 从这里安装 GLEW 。
- 从这里安装 GLFW 。
- 解压 GLEW 和 GLFW
- 进入 GLEW 文件夹
- 运行
make
,sudo make install
并且make clean
(按照此) - 进入 GLFW 文件夹
- 运行
mkdir build
,cd build
并且cmake ..
(在此之后)
这就是棘手的地方。 - 运行
mkdir test-project
,mkdir test-project/dependencies
,mkdir test-project/dependencies/include
, mkdirtest-project/dependencies/lib
- 将
GLFW
GLFW 包含目录中的文件夹移动到项目包含目录
我不知道我应该链接什么,所以我跳过了这部分。 - 将
GL
GLEW 包含目录中的文件夹移动到项目包含目录 - 将
lib
文件夹内容移动到项目lib
目录 main.cpp
在项目文件夹中创建一个文件- 将此添加到
main.cpp
:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main()
{
return 0;
}
- 转到项目文件夹并执行
g++ ./main.cpp -I ./dependencies/include -L ./dependencies/lib
,然后我收到此错误:
In file included from ./main.cpp:1:
./dependencies/include/GL/glew.h:1205:14: fatal error: GL/glu.h: No such file or directory
1205 | # include <GL/glu.h>
| ^~~~~~~~~~
compilation terminated.
请有人解释 C++ 库如何动态和静态地工作,如何动态和静态地安装库,并提供一些在 Ubuntu 上使用 GLEW 和 GLFW 的步骤。
任何帮助将不胜感激!
解决方案
由于您使用的是 Ubuntu,因此您可以使用 apt 来安装这些库。
sudo apt install libglfw3-dev
sudo apt install libglew-dev
sudo apt install libgl1-mesa-dev
在这些之后,您应该在系统上安装 GLEW 和 GLFW。为了构建,我会使用 CMake。只需具有此文件夹结构:
├── build
├── CMakeLists.txt
└── main.cpp
哪里CMakeLists.txt
是:
cmake_minimum_required(VERSION 3.17)
project(test LANGUAGES CXX)
add_executable(test main.cpp)
target_link_libraries(test glfw GLEW /usr/lib/x86_64-linux-gnu/libGL.so.1)
并且main.cpp
是:
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main()
{
if (!glfwInit()) {
fprintf(stderr, "ERROR: could not start GLFW3\n");
return 1;
}
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
if (!window) {
fprintf(stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit();
const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
const GLubyte* version = glGetString(GL_VERSION); // version as a string
printf("Renderer: %s\n", renderer);
printf("OpenGL version supported %s\n", version);
glEnable(GL_DEPTH_TEST); // enable depth-testing
glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
glfwTerminate();
return 0;
}
为了构建这个应用程序
cd build
cmake ..
make
./test
PS:示例代码取自这里:https ://antongerdelan.net/opengl/hellotriangle.html
更新:CMake 搜索库的默认路径。在 Unix 系统中,它们是/usr/local/lib, /usr/lib/x86_64-linux-gnu/, /usr/lib
etc。包含目录 :也是如此/usr/include
,这些是通过包管理器安装的库使用的路径。如果您想包含来自不同路径的库。让我们做一个简单的例子:/usr/include/x86_64-linux-gnu/
/usr/local/include
我写了一个库并将其发布为:
.
├── CMakeLists.txt
├── untitled.cpp
└── untitled.h
其中无标题.h:
#pragma once
class MyLibrary
{
public:
MyLibrary();
void doWork();
};
和无标题.cpp:
#include <untitled.h>
#include<iostream>
MyLibrary::MyLibrary()
{
}
void MyLibrary::doWork()
{
std::cout<<"do work is called"<<std::endl;
}
和我的 CMakeLists.txt :
cmake_minimum_required(VERSION 3.14)
project(untitled LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_library(untitled SHARED
untitled.h
untitled.cpp
) # I am generating shared library from these source files.
# Then I am telling cmake to generate Makefile so that when user does make install
# my library will be installed to folder /usr/local/untitled/
set_target_properties(untitled PROPERTIES PUBLIC_HEADER "untitled.h")
INSTALL(TARGETS untitled
LIBRARY DESTINATION untitled/lib
PUBLIC_HEADER DESTINATION untitled/include)
现在,作为我的库的用户,您下载了源代码并希望为您的系统构建。你跟着
mkdir build && cd build && cmake .. && make
然后你跑sudo make install
。输出如下:
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/untitled/lib/libuntitled.so
-- Installing: /usr/local/untitled/include/untitled.h
然后你想在你的项目中使用这个库。
cmake_minimum_required(VERSION 3.17)
project(test LANGUAGES CXX)
add_executable(test main.cpp)
# You should add these lines to your CMake file because now my library lives in an unusual path
target_include_directories(test PUBLIC /usr/local/untitled/include)
target_link_directories(test PUBLIC /usr/local/untitled/lib)
# And I have updated link_libraries section with X because there is an libX.so file
# under any of these library paths: /usr/local/lib, /usr/lib (default paths) and custom
# added library path : (usr/local/untitled/lib). In this case X is untitled.
target_link_libraries(test glfw GLEW /usr/lib/x86_64-linux-gnu/libGL.so.1 untitled)
我正在使用库:
#include <untitled.h>
int main()
{
MyLibrary ml;
ml.doWork();
return 0;
}
推荐阅读
- android - 如何将带有 POST 数据的 URL 加载到 GeckoView
- aggregate-functions - 如何在 Quicksight 中使用 sumover 在 2 个字段上模拟分组
- c# - 如何使用扩展方法跟踪任务的状态?
- node.js - “未能读取请求表单。缺少内容类型边界。”。爱讯。做出反应。节点,表达
- linux - 如何在bash中grep和替换字符串
- node.js - 如何在 sinon 中测试 axios 的调用获取?
- python - 我在从 Django 模型中传递模板中的特定图像时遇到问题
- javascript - Javascript在具有相同类但变量不同的多个div中运行函数
- javascript - 我想通过获取成功注册后设置的 URL 参数来更改或删除我的 html 元素
- python - 无法通过 pip 在 macOS Big Sur 上安装 jamspell?