inlineのせいだった
//a.h #include <string> using namespace std; class A{ public: int print_str(const string *str) const; };
//a.cpp #include <stdio.h> #include "a.h" inline int A::print_str(const string *str) const { printf("%s", str->c_str()); }
//b.cpp #include "a.h" int main(){ A *a = new A(); string str; str = "hello"; a->print_str(&str); }
の三つのファイルがあって、
$ g++ -c -O2 a.cpp $ g++ -c -O2 b.cpp $ g++ -O2 a.o b.o
ってやると、
c.o: In function `main': c.cpp:(.text+0x53): undefined reference to `A::print_str(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const*) const' collect2: ld はステータス 1 で終了しました
ってなって、1時間ぐらいはまってたのでメモ。最適化をかけなかったら普通に成功する。
原因はa.cppのinline指定で、inlineを消せば良かった。最初は型が違うのかなーと思ってたけど、そうでもなかったらしい。
inlineって書いてできなかったことが前にもあるので、とりあえずはつけないでいたほうがいいのかもしれない。