静态变量链接错误
static variable link error
我正在mac上编写C++代码。为什么在编译时会出现此错误?:
体系结构i386的未定义符号:"日志:字符串",引用自:在libTest.a(Log.o)ld:symbol中找不到体系结构i386的Log::方法(std::string)clang:error:链接器命令失败退出代码1(使用-v查看调用)
不确定我的代码是否错误,或者我必须向Xcode添加额外的标志。我当前的XCode配置是"静态库"项目的默认配置。
我的代码:
日志----------------
#include <iostream>
#include <string>
using namespace std;
class Log{
public:
static void method(string arg);
private:
static string theString ;
};
Log.cpp----
#include "Log.h"
#include <ostream>
void Log::method(string arg){
theString = "hola";
cout << theString << endl;
}
我从测试代码中调用"方法",方法如下:'日志::方法("asd"):'
谢谢你的帮助。
您必须在cpp
文件中定义静态。
Log.cpp
#include "Log.h"
#include <ostream>
string Log::theString; // <---- define static here
void Log::method(string arg){
theString = "hola";
cout << theString << endl;
}
您还应该从标头中删除using namespace std;
。趁你还可以养成这个习惯。这将用std
污染全局命名空间,无论您在哪里包含头。
您声明了static string theString;
,但尚未定义它。
包括
string Log::theString;
到您的cpp
文件
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 使用cmake错误链接boost日志
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- 静态库中的 g++ 错误链接函数
- 未定义的参考错误链接天然函数Android
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- QT Q_PROPERTY错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- C 错误链接器命令失败了出口代码1(使用-V查看调用)
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See
- 聚输出错误 - 链接列表
- Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用)
- 尝试构建C DLIB示例无法与数百个未定义的参考错误链接