C++ C+中未解析的外部符号+;标题

C++ C+中未解析的外部符号+;标题,c++,header,googletest,C++,Header,Googletest,xyz.cpp现在为test.hpp中定义的每个变量抛出一个未解析的外部符号,尽管其中包括test.hpp。不知道为什么。任何帮助都将不胜感激 Test.hpp: #ifndef TEST_H #define TEST_H #include <time.h> #include <tuple> #include "gtest/gtest.h" #include "gmock/gmock.h" // Simple header to allow the transfer

xyz.cpp现在为test.hpp中定义的每个变量抛出一个未解析的外部符号,尽管其中包括test.hpp。不知道为什么。任何帮助都将不胜感激

Test.hpp:

#ifndef TEST_H
#define TEST_H

#include <time.h>
#include <tuple>

#include "gtest/gtest.h"
#include "gmock/gmock.h"

// Simple header to allow the transfer of command line parameters between main.cpp (where google test is run)
// and TestDuration.cpp (where the test itself is defined).
#define INTERACTIVE 0
#define COMMAND_LINE 1
#define STATIC 2

extern int runType;
extern bool clInputGiven;    // Is True only if there is command line input intended for a test.
extern double clDuration;
extern int clIterations;

#endif
xyz.cpp

...
if (runType == INTERACTIVE) {
                // Test block. Expects no throw for every iteration of calib/meas X times.
                EXPECT_NO_THROW({
...
xyz.cpp和main.cpp只是在开头有一个简单的#include“test.h”

我的问题: 1.我假设我不理解include是如何工作的,所以任何深入的解释都会有帮助。 2.如果头文件方法将导致太多问题,您将如何将参数从main.cpp传递到xyz.cpp


如果你需要更多的信息,尽管问。提前谢谢你的帮助

没关系,经过足够的实验才弄明白。感谢@Ron的帮助。我很抱歉重新措辞了这个问题(不知道它会抹掉你的答案)

如果你遇到同样的问题

最后,我将#pragma放在头文件的顶部,在头文件中用extern声明每个变量,然后在main.cpp的全局范围(main方法的外部/上方)中显式定义每个变量

pragma once告诉预处理器在编译中只包含一次文件。它的作用与include-guard相同,但代码较少。Extern告诉程序变量是在代码的其他地方定义/存储的


希望这能帮助将来遇到同样问题的人

我们需要看看你是如何编译的,还有一个。还有,有些东西告诉我gtest是不可逆转的。@HolyBlackCat我本想重做一次,但我发现了。我使用cmake来构建它,尽管我验证了构建工具不是问题所在。Gtest是相关的,因为原始问题(Gtest不允许您直接将命令行参数传递给测试本身,所以我试图通过使用标题找到解决方法。Hacky我知道,但这是我能想到的最好的方法,同时仍然提供Gtest提供的实用程序)。Ron可能手动删除了他的答案。你不能用编辑来做那件事,那太傻了。
...
if (runType == INTERACTIVE) {
                // Test block. Expects no throw for every iteration of calib/meas X times.
                EXPECT_NO_THROW({
...