私のコードは次のとおりです。
test.cpp
#include<iostream>
#include<boost/bind.hpp>
#include "extern.h"
using namespace std;
using namespace boost;
int fun(int x,int y){return x+y;}
/*
*void add(int &m,int &n);
*/
int main(){
int m=1;int n=2;
cout << m << " "<< n << endl;
add(m,n);
cout << m << " "<< n << endl;
return 0;
}
extern.h:
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
void add(int &n,int &m);
extern.cpp:
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
extern int m;
extern int n;
void add(int &n,int &m) {
n = n+1;
m = m+1;
}
でコンパイルすると
g++ -Wall -o test test.cpp
次のようになります。
/tmp/ccMHVRNo.o: In function `main':
test.cpp:(.text+0x7b): undefined reference to `add(int&, int&)'
collect2: ld returned 1 exit status
しかし、私がそれをコンパイルすると:
g++ -Wall -o test test.cpp extern.cpp
それはうまくいきます:
$ ./test
1 2
2 3
その理由は、関数test.cpp
の実装が見つからないことです。add()
しかし、追加extern.h
しましたがtest.cpp
、「未定義の参照」とまだ表示されているのはなぜadd(int&, int&)
ですか?