相关疑难解决方法(0)

静态变量链接错误

我在mac上编写C++代码.编译时为什么会出现此错误?:

体系结构i386的未定义符号:"Log :: theString",引用自:libTest.a中的Log :: method(std :: string)(Log.o)ld:未找到体系结构i386 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)

不确定我的代码是否错误或者我必须向Xcode添加其他标志.我当前的XCode配置是"静态库"项目的默认配置.

我的代码:

Log.h ------------

#include <iostream>
#include <string>

using namespace std;

class Log{
public:
    static void method(string arg);
private:
    static string theString ;
};
Run Code Online (Sandbox Code Playgroud)

Log.cpp ----

#include "Log.h"
#include <ostream>

void Log::method(string arg){
    theString = "hola";
    cout   << theString << endl; 
}
Run Code Online (Sandbox Code Playgroud)

我用测试代码调用'方法',这样:'Log :: method("asd"):'

谢谢你的帮助.

c++ xcode static-methods clang static-libraries

63
推荐指数
2
解决办法
3万
查看次数

未定义的静态变量引用

可能重复:
C++:对静态类成员的未定义引用

我正在使用MinGW.为什么静态变量不起作用

[Linker error] undefined reference to `A::i' 

#include <windows.h>

    class A { 
        public:     
        static int i;
        static int init(){

            i = 1;  

        }

    };

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil){
    A::i = 0;
    A::init();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

23
推荐指数
2
解决办法
6万
查看次数

标签 统计

c++ ×2

clang ×1

static-libraries ×1

static-methods ×1

xcode ×1