我知道我在这里一定做错了什么。
rank.h
#ifndef RANK_H
#define RANK_H
namespace mmi {
int chunk;
void rank(int my_rank);
}
#endif
rank.cpp
#include "rank.h"
namespace mmi {
//do something with chunk
}
main.cpp
#include "rank.h"
int main() {
mmi::chunk = 1;
}
以及编译的输出;
g++ -g -Wall -std=gnu++11 -c -o main.o main.cpp
g++ -g -Wall -std=gnu++11 -c -o rank.o rank.cpp
mpic++ main.o rank.o -o main
rank.o:(.bss+0x0): multiple definition of `mmi::chunk'
main.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'main' failed
make: *** [main] Error 1
我的理解是头文件被包含多次。但我希望通过使用来纠正这个问题#ifndef
.
那么,我可以问一下这是怎么回事吗?
The line
int chunk;
不仅是一个声明,它也是一个定义。每个 .cpp 文件#include
.hpp 文件最终定义了它。
将其更改为
extern int chunk;
然后,确保在 .cpp 文件中定义它。
rank.cpp
#include "rank.h"
namespace mmi {
int chunk;
//do something with chunk
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)