Skip to content

Instantly share code, notes, and snippets.

@morrisonlevi
Created July 19, 2018 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morrisonlevi/b1508531b1464921664ca06c0fd889bb to your computer and use it in GitHub Desktop.
Save morrisonlevi/b1508531b1464921664ca06c0fd889bb to your computer and use it in GitHub Desktop.
CMake 3.12: Transitive OBJECT Libraries issue
#include "box.hh"
template class box<int>;
#ifndef BOX_HH
#define BOX_HH 1
#include <utility>
template <class T> class box {
T item_;
public:
box(T t) : item_{std::move(t)} {}
~box() = default;
T get() const { return item_; }
};
#endif
[ 75%] Linking CXX executable main
/zapps/cmake/3.12.0/gcc-7.3.0/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/apps/gcc/7.3.0/bin/c++ -g CMakeFiles/main.dir/main.cc.o CMakeFiles/make_box.dir/make_box.cc.o -o main
CMakeFiles/main.dir/main.cc.o: In function `main':
/fslhome/levijm/compute/cmake-3.12-issue/main.cc:9: undefined reference to `box<int>::get() const'
CMakeFiles/make_box.dir/make_box.cc.o: In function `make_box(int)':
/fslhome/levijm/compute/cmake-3.12-issue/make_box.cc:6: undefined reference to `box<int>::box(int)'
collect2: error: ld returned 1 exit status
make[2]: *** [main] Error 1
cmake_minimum_required(VERSION 3.12)
project(objectlib LANGUAGES CXX)
add_library(box OBJECT box.cc box.hh)
add_library(make_box OBJECT make_box.cc make_box.hh)
target_link_libraries(make_box PUBLIC box)
add_executable(main main.cc)
target_link_libraries(main PUBLIC make_box)
#include "box.hh"
#include "make_box.hh"
extern template class box<int>;
int main() {
box<int> b = make_box(0);
return b.get();
}
#include "make_box.hh"
#include "box.hh"
extern template class box<int>;
box<int> make_box(int i) { return box<int>{i}; }
#ifndef MAKE_BOX_HH
#define MAKE_BOX_HH 1
#include "box.hh"
extern template class box<int>;
box<int> make_box(int i);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment