C++ 头文件中未解析的外部符号
Unresolved External Symbol in C++ Header
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
main.cpp :
...
int runType = -1;
bool clInputGiven = false;
double clDuration = -1.0;
int clIterations = -1;
...
if (ac == 4 && av[1] == "--cl_mode")
{
runType = COMMAND_LINE;
clInputGiven = true;
clIterations = atoi(av[2]);
clDuration = atoi(av[3]);
}
...
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. 我假设我不理解 includes 是如何工作的,所以任何深入的解释都会有所帮助。
2.如果头文件方法会导致太多问题,你如何将参数从main.cpp传递到xyz.cpp?
如果您需要更多信息,请直接询问。提前感谢您的帮助!
没关系,经过足够多的实验后弄明白了。感谢@Ron 的帮助。很抱歉重新措辞了这个问题(不知道它会抹掉你的答案)。
如果你运行遇到同样的问题...
我最终将#pragma once 放在头文件的顶部,在头文件中用 extern 声明每个变量,然后在 main.cpp 的全局范围内显式定义每个变量(在主要方法)。
pragma once 告诉预处理器在编译中只包含一次文件。它的行为与 include guards 相同,但代码更少。 Extern 告诉程序该变量已定义/存储在代码中的其他位置。
希望对以后 运行 遇到同样问题的任何人有所帮助!
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
main.cpp :
...
int runType = -1;
bool clInputGiven = false;
double clDuration = -1.0;
int clIterations = -1;
...
if (ac == 4 && av[1] == "--cl_mode")
{
runType = COMMAND_LINE;
clInputGiven = true;
clIterations = atoi(av[2]);
clDuration = atoi(av[3]);
}
...
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. 我假设我不理解 includes 是如何工作的,所以任何深入的解释都会有所帮助。 2.如果头文件方法会导致太多问题,你如何将参数从main.cpp传递到xyz.cpp?
如果您需要更多信息,请直接询问。提前感谢您的帮助!
没关系,经过足够多的实验后弄明白了。感谢@Ron 的帮助。很抱歉重新措辞了这个问题(不知道它会抹掉你的答案)。
如果你运行遇到同样的问题...
我最终将#pragma once 放在头文件的顶部,在头文件中用 extern 声明每个变量,然后在 main.cpp 的全局范围内显式定义每个变量(在主要方法)。
pragma once 告诉预处理器在编译中只包含一次文件。它的行为与 include guards 相同,但代码更少。 Extern 告诉程序该变量已定义/存储在代码中的其他位置。
希望对以后 运行 遇到同样问题的任何人有所帮助!