【问题标题】:How do I use a static library (in my case assimp) without the source files如何在没有源文件的情况下使用静态库(在我的情况下为 assimp)
【发布时间】:2016-02-05 16:36:11
【问题描述】:

我有一个 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 的路径。

EDIT2:

我将我的 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。它仍然不起作用,和以前一样的错误。 我的 main.cpp 看起来像这样:

#include <assimp/Importer.hpp>
#include <cstdlib>

int main() {
    Assimp::Importer importer;
    return EXIT_SUCCESS;
}

EDIT3:

我认为这与 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'

【问题讨论】:

  • 你能发布你是如何编译你的项目的吗?
  • 你错过了cmake标签。
  • 直接使用CMAKE_CXX_FLAGS 可能不是你想要的。您应该使用 include_directories 添加包含目录。您在其他地方使用${ASSIMP_INCLUDE_DIR} 吗?
  • 我将其更改为include_directories,但我遇到了同样的错误,一定是别的错误。是的,我将它用于我的ExternalProject_Add_Step 将标题移动到正确的文件夹。
  • @mistapink:我添加了我的 main.cpp 和 CMake 文件的最小工作示例。

标签: c++ cmake static-libraries assimp


【解决方案1】:

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

target_link_libraries(monoRenderer z)

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

#include <Importer.hpp>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多