如何在没有源文件的情况下使用静态库(在我的例子中是 assimp)

How do I use a static library (in my case assimp) without the source files

本文关键字:我的 assimp 源文件 情况下 静态      更新时间:2023-10-16

我有一个libassimp.a文件和头文件。如何使用图书馆?

我正在使用set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem ${ASSIMP_INCLUDE_DIR}")将头文件添加到我的项目中。随着ASSIMP_INCLUDE_DIR是../contrib/assimp/include.

现在,当我在main中使用某个类时.cpp它会给我一些关于对某些功能的未定义引用的错误,因为显然我没有源文件。

当我将 libassimp.a 添加到我的编译标志时,我在使用 make 时出现以下错误:

make[3]: *** No rule to make target `../contrib/assimp/lib/libassimp.a',
...
main.cpp:7:32: fatal error: assimp/Importer.hpp: No such file or directory
....
Linking CXX static library libassimp.a

我不明白这些信息。也许他们在那里是因为它试图在它实际存在之前访问libassimp.a?这是某种并发问题吗?

无论如何,如果我再次调用make,则会出现不同的错误,即对我没有使用的东西的大量未定义的引用,例如

../contrib/assimp/lib/libassimp.a(AssbinLoader.cpp.o): In function `Assimp::AssbinImporter::InternReadFile(std::string const&, aiScene*, Assimp::IOSystem*)':
AssbinLoader.cpp:(.text+0x2a49): undefined reference to `uncompress'

编辑:

我用CMake编译是这样的:

target_link_libraries(monoRenderer [some other libraries] ${ASSIMP_STATIC_LIB})

ASSIMP_STATIC_LIB是通往libassimp.a的道路。

编辑2:

我将我的 CMake 文件简化为:

cmake_minimum_required(VERSION 2.8.12)
project(monoRenderer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
file(GLOB_RECURSE CXX_SRCS src/*.cpp)
file(GLOB_RECURSE C_SRCS src/*.c)
file(GLOB_RECURSE HPP_HDRS src/*.hpp)
file(GLOB_RECURSE H_HDRS src/*.h)
set(SRCS "${C_SRCS};${CXX_SRCS}")
set(HDRS "${H_HDRS};${HPP_HDRS}")
include_directories(${PROJECT_SOURCE_DIR}/contrib/assimp/include)
add_executable(monoRenderer ${SRCS} ${HDRS})
target_link_libraries(monoRenderer ${PROJECT_SOURCE_DIR}/contrib/assimp/lib/libassimp.a)

头文件在contrib/assimp/include中,libassmip.a 在contrib/assimp/lib 中。它仍然不起作用,与以前相同的错误。我的主.cpp看起来像这样:

#include <assimp/Importer.hpp>
#include <cstdlib>
int main() {
    Assimp::Importer importer;
    return EXIT_SUCCESS;
}

编辑3:

我认为

这与 zlib 有关,因为我认为所有错误似乎都有共同点:

undefined reference to `uncompress'
undefined reference to `inflateInit2_'
undefined reference to `inflate'
undefined reference to `inflateEnd'
undefined reference to `inflateReset'
undefined reference to `inflateSetDictionary'
undefined reference to `get_crc_table'
undefined reference to `crc32'

正如你自己所说,你遇到了zlib的问题。您必须自己添加静态库中的所有依赖项,例如:

target_link_libraries(monoRenderer z)

由于您声明标题位于contrib/assimp/include您可能希望将包含main.cpp更改为

#include <Importer.hpp>