私は次の3つのファイルを持っています:
error.h
#ifndef error_h
#define error_h
#include <string>
#include <iostream>
#include <cstdio>
void Error(std::string msg);
#endif
error.cpp
#ifdef error_h
#include "error.h"
void Error(std::string msg)
{
std::cerr
<< "\n=========================================================\n"
<< msg
<< "\n=========================================================\n";
exit(EXIT_FAILURE);
}
#endif
foobar.cpp
#include "error.h"
int main()
{
for(int i=0; i<99; i++)
if(i==55)
Error("this works");
return 0;
}
今私がやります:
$ g++ -c error.cpp foobar.cpp
$ g++ error.o foobar.o -o exampleprogram
そして私は得る:
foobar.o: In function `main':
foobar.cpp:(.text+0x4b): undefined reference to `Error(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
私は何が間違っているのですか?これを解決するために何を理解する必要がありますか、そして将来、質問せずに同様の問題が発生しますか?ありがとう!