【发布时间】:2017-03-31 15:00:39
【问题描述】:
我正在尝试将一些实用程序函数和全局变量添加到我的代码中,这样我就可以在项目中我想要的每个类中使用它们。我想使用 .hpp 文件进行定义,使用 .cpp 文件进行实施。
这是这两个文件的摘要:
// 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 标头。
现在我认为我可以在Class1.hpp 或Class2.hpp 中添加另一个#include "Utilities.hpp" 而不会出现任何问题,因为我在该标题中使用了防护。问题是,当我尝试这样做时,链接器会向我抛出此错误:Apple Mach-O Linker (ld) Error Group - clang: error: linker command failed with exit code 1 (use -v to see invocation),我不明白为什么或如何解决它。
我在 macOS Sierra 10.12.4 上使用 Xcode 8.3。
我希望我能够解释我的问题,非常感谢你们。
【问题讨论】:
-
它是否提供更详细的输出?另外,请尝试清理并重建您的项目。