首页 > 解决方案 > 在 Linux 上动态和静态地使用 C++ 库

问题描述

如果这是一个重复的问题,我深表歉意,但我无法在任何地方找到答案。

我是 C++ 新手,希望开始学习 OpenGL。为此,我需要同时设置GLEWGLFW。尽管阅读了文档并进行了大量研究,但我无法弄清楚如何静态和动态地利用 C++ 库。我找不到答案的主要原因是因为我在 Ubuntu 上运行,而大多数资源都是在 Windows 上运行的。

我已尝试使用CMake构建库,遵循文档。我似乎成功地构建了这些库,但是当将这些库与编译器一起使用时,问题就出现了,我再次找不到足够好的答案。

我尝试了以下步骤来安装 GLEW 和 GLFW:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main()
{
    return 0;
}
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 的步骤。

任何帮助将不胜感激!

标签: c++glfwglew

解决方案


由于您使用的是 Ubuntu,因此您可以使用 apt 来安装这些库。

  1. sudo apt install libglfw3-dev

  2. sudo apt install libglew-dev

  3. 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;
}

为了构建这个应用程序

  1. cd build
  2. cmake ..
  3. make
  4. ./test

PS:示例代码取自这里:https ://antongerdelan.net/opengl/hellotriangle.html

更新:CMake 搜索库的默认路径。在 Unix 系统中,它们是/usr/local/lib, /usr/lib/x86_64-linux-gnu/, /usr/libetc。包含目录 :也是如此/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;
}

要查看系统中包含目录的位置,请参阅此答案 ,并查看系统中的库在哪里,请参阅此答案


推荐阅读