在main.cpp以外的文件中包含标头时,链接器错误 [英] Linker error when including header in files other than main.cpp
问题描述
我正在尝试以某种方式在代码中添加一些实用程序函数和全局变量,以便可以在项目中所需的每个类中使用它们.我想在定义时使用.hpp文件,在实现时使用.cpp文件结束.
I'm trying to add some utility functions and global variables to my code in such a way that I can be able to use them in every class I want in my project. I would like to use a .hpp file for the definitions end a .cpp file for the implementation.
这是这两个文件的摘要:
This is a summary of these two files:
// This is Utilities.hpp
#ifndef utilities_hpp
#define utilities_hpp
namespace utils {
int global_variable1;
int global_variable2;
void utility_function1(...);
void utility_function2(...);
void utility_function3(...);
}
#endif /* utilities_hpp */
和实现:
// This is Utilities.cpp
#include "Utilities.hpp"
namespace utils {
void utility_function1(...) {
// Some code
}
void utility_function2(...) {
// Some code
}
void utility_function3(...) {
// Some code
}
}
除了我的 main.cpp
文件外,我还有另外两个类.我的 main.cpp
文件包含 Class1.hpp
标头,其中包含 Class2.hpp
标头.
Other than my main.cpp
file I have two other classes. My main.cpp
file includes Class1.hpp
header that includes Class2.hpp
header.
现在我认为我可以在 Class1.hpp
或 Class2.hpp
中放置另一个 #include"Utilities.hpp"
因为我已经在标题中使用了警卫.问题是,当我尝试执行此操作时,链接器将向我抛出此错误: Apple Mach-O Linker(ld)错误组-铛:错误:链接器命令失败,退出代码为1(使用-v查看调用)
,我不明白为什么或怎么解决它.
Now I thought that I could put another #include "Utilities.hpp"
in Class1.hpp
or Class2.hpp
without any problems since I've used the guards in that header. The thing is that when I try to do that the linker throws me this error: Apple Mach-O Linker (ld) Error Group - clang: error: linker command failed with exit code 1 (use -v to see invocation)
and I can't understand why or what to do to solve it.
我正在macOS Sierra 10.12.4上使用Xcode 8.3.
I'm using Xcode 8.3 on a macOS Sierra 10.12.4.
希望我能解释我的问题,非常感谢.
I hope I was able to explain my problem, thank you all very much in advance.
推荐答案
您违反了单一定义规则.应该在标头中将 global_variable1
和 global_variable2
声明为 extern
,并在一个翻译单元(可能是Utilities.cpp)中进行定义.
You've violated the One Definition Rule. global_variable1
and global_variable2
should be declared extern
in your header and defined in exactly one translation unit (probably Utilities.cpp).
您已经在一个标头中定义了全局变量,该标头包含在多个转换单元中,因此在main.cpp中定义了一个 utils :: global_variable1
,在Utilities.cpp中定义了一个.关于链接时间,链接器无法知道要使用哪个 global_variable1
,因此会引发错误.
You've defined global variables in a header that gets included in multiple translation units, so there's a utils::global_variable1
defined in main.cpp, and one in Utilities.cpp. When it comes to link time, the linker has no way to know which global_variable1
to use, so it throws an error.
要解决此问题,请将 extern
关键字添加到声明中,然后在"Utilities.cpp"中添加定义:
To fix it, add the extern
keyword to your declarations and add a definition in "Utilities.cpp":
Utilities.hpp:
Utilities.hpp:
// This is Utilities.hpp
#ifndef utilities_hpp
#define utilities_hpp
namespace utils {
extern int global_variable1;
//^^^^^^ <-----HERE
extern int global_variable2;
//^^^^^^ <-----HERE
void utility_function1(...);
void utility_function2(...);
void utility_function3(...);
}
#endif /* utilities_hpp */
Utilities.cpp:
Utilities.cpp:
// This is Utilities.cpp
#include "Utilities.hpp"
namespace utils {
int global_variable1; //<---- Definitions added
int global_variable2;
void utility_function1(...) {
// Some code
}
void utility_function2(...) {
// Some code
}
void utility_function3(...) {
// Some code
}
}
这篇关于在main.cpp以外的文件中包含标头时,链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!