g++无法将cpp文件编译为含有代码的o文件(Please Ignore Negative words, or replace them to other words, thanks.)
比如如下有3个文件: m.hpp
, m.cpp
, main.cpp
m.hpp文件内容如下
namespace testing
{
int func(int a, int b);
}
m.cpp文件内容如下
#include "./m.hpp"
namespace testing
{
int func(int a, int b)
{
return a + b;
}
}
main.cpp
#include <iostream>
#include "./m.hpp"
int main(void)
{
std::cout << testing::func(1, 2) << std::endl;
return 0;
}
然后进行编译和连接
g++ -c main.cpp -o main.o
g++ main.o m.o -o main
就可以得到
undefined reference to `testing::func(int, int)'
将命名空间去掉可以解决。
(但是这样我又为什么要写C++呢?)