0

I am building a shared library that instantiates some template code. This template code uses other templates (traits). In the corresponding .cpp I explicitly instantiate the templates that I want to use from that shared library.

After this, I link against this library and I use those instantiated functions, but I get a linker error.

Sample code:

include/shared.cpp

#include <iostream>
#include "otro.hpp"

template<class T>
void tal(){
    std::cout << f<T, int>::value << std::endl;
}

include/otro.hpp

template<class T, class U>
struct f{};

template<class U>
struct f<int, U>{
    static constexpr char value[] = "chars";
};

src/shared.cpp

#include "shared.hpp"
template void tal<int>();

src/main.cpp

#include "shared.hpp"
int main(){
    tal<int>();
}

src/CMakeLists.txt

add_library(${PROJECT_NAME} SHARED shared.cpp)
add_executable(foo main.cpp)
target_link_libraries(foo PRIVATE ${PROJECT_NAME})

Remark: If I change the type of the trait f to int it works perfectly well.

Lezkus
  • 160
  • 1
  • 12

0 Answers0