Search code examples
c++cmakestatic-librariesassimp

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


I have a libassimp.a file and the header files. How can I use the library?

I'm adding the header files to my project with set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem ${ASSIMP_INCLUDE_DIR}"). With ASSIMP_INCLUDE_DIR being ../contrib/assimp/include.

Now when I use some class in my main.cpp it gives me errors about undefined references to some funtions, because obviously I don't have the source files.

When I add the libassimp.a to my compile flags I get the following errors when using 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

I don't understand these messages. Maybe they are there because it's trying to access libassimp.a before it's actually there? Is this some kind of concurrency problem?

Anyways, if I call make again then I get different errors, namely a bunch of undefined references to things I am not using, e.g.

../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'

EDIT:

I am compiling with CMake like this:

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

ASSIMP_STATIC_LIB is the path to libassimp.a.

EDIT2:

I reduced my CMake file to this:

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)

The header files are in contrib/assimp/include and the libassmip.a is in contrib/assimp/lib. It still doesn't work, same errors as before. My main.cpp looks like this:

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

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

EDIT3:

I think it has something to do with zlib, since all the errors seem to have that in common I think:

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'

Solution

  • As you stated yourself you are encountering problems with zlib. You have to add all dependencies from your static library yourself, e.g.:

    target_link_libraries(monoRenderer z)
    

    Since you stated that your header are located in contrib/assimp/include you might want to change your include in main.cpp to

    #include <Importer.hpp>