我正在尝试用C++做一些非常简单的东西,但我找不到任何关于如何解决这个问题的信息.即使是我刚刚写的那本书"只需编译并运行程序".
TEST.CPP
#include <iostream>
using namespace std;
int main()
{
cout << "Never fear, C++ is here!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器说:
Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccVfJHGs.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccVfJHGs.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccVfJHGs.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccVfJHGs.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我试图像标志编译-arch i386和-m32,但它总是说,这是错误的架构.我应该使用哪一个?
我在Mac上做这个但不使用XCode,只是gcc.
为什么静态数据成员初始化必须在类之外?
class X
{
public:
int normalValue = 5; //NSDMI
static int i;
};
int X::i = 0;
Run Code Online (Sandbox Code Playgroud)
为什么静态数据成员(此处为"i")只是一个声明,而不是一个定义?
我在Mac OS X上使用Qt 4.7和Cmake 2.8.3以及g ++ 4.2.1.
在我的一个文件中使用静态或全局变量时,我遇到了一个奇怪的链接器错误.这是错误:
ld: duplicate symbol ColorTrail::calculateColorUniformLocation in CMakeFiles/GLBall.dir/src/DesktopMain.cpp.o and CMakeFiles/GLBall.dir/src/ColorTrail.cpp.o
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
calculateColorUniformLocation是ColorTrail类的静态成员......但它甚至根本不在DesktopMain.cpp中使用!
这是我尝试过的:重命名变量并不能解决问题.将变量移出类并使其成为普通的全局变量也无法解决问题
文件ColorTrail.h:
#ifndef COLORTRAIL
#define COLORTRAIL 9
#include "GlobalConstants.h"
#include <vector>
using namespace std;
class ColorTrail
{
private:
//note that this is NOT a Q_OBJECT
static GLint calculateColorUniformLocation;
//omitted for brevity
};
GLint ColorTrail::calculateColorUniformLocation;
#endif
Run Code Online (Sandbox Code Playgroud)
DesktopMain.cpp使用ColorTrail类,但不是静态而且从不引用变量.
任何人都知道什么可能是错的/ Qt有类似的问题?