Problems with linking files (Undefined symbols for architecture x86_64)

Started by
4 comments, last by SpaceEmotion 11 years, 8 months ago
I recently started learning C++ (I already know a lot about PHP and Java and have some knowledge in C) to create some games and discovered the SDL framework. After watching some tutorials in youtube I wanted to start myself making some basic stuff so I started setting up the environment using Netbeans.

First test were successfully (SDL_Init() and stuff) so I wanted to create some static classes (like i know them in PHP or Java) to create some Console output (using a prefix, date/time and so on) so I had three files in the end (console.h, console.cpp and main.cpp).

I got no compile errors but the linker said the following:

Undefined symbols for architecture x86_64:
"Console::prefix", referenced from:
Console::setPrefix(std::string) in console.o
Console::log(std::string) in console.o
ld: symbol(s) not found for architecture x86_64


Because I am a mac user I also tried using XCode (version 4), the same errors again. I don't know if its my code or just another problem, I really hope someone can help me in here.

For reference, this is the code I am using:

main.cpp ( the char** was added due to a tip in stackoverflow when I googled my problem, it is from my test with XCode)

#include <SDL/SDL.h>
#include <iostream>
#include "console.h"

int main(int argc, char** argv) {
Console::setPrefix("[Game]");

Console::log("Initializing SDL");
SDL_Init(SDL_INIT_EVERYTHING);

Console::log("Shutting down SDL");
SDL_Quit();

return 0;
}


console.h

#ifndef CONSOLE_H
#define CONSOLE_H
#include <string>
using namespace std;

class Console {
private:
/**
* The prefix used in the log output
*/
static string prefix;

public:
/**
* Sets the prefix for the output
* @param msg
*/
static void setPrefix(string msg);

/**
* Echoes the message with the prefix to the console
* @param msg
*/
static void log(string msg);
};
#endif


console.cpp

#include <iostream>
#include <string>
#include "console.h"
using namespace std;

void Console::setPrefix(string msg) {
prefix = msg;
}

void Console::log(string msg) {
std::cout << prefix << " " << msg;
}



(I don't know if its right to place this topic in here but since its a "beginners" problem I think this would be the best place in the forums.)
Advertisement
If I remember correctly, I had, some years back, to compile sdl the usual Linux/Unix way as lib. The framework could be broken or needs something special.
You have declared your static variable Console::prefix, but you have yet to define it. The definition goes in a single source file (the obviously candidate being console.cpp) and looks like this:

string Console::prefix;


You may want to initialise it with a default prefix:

string Console::prefix = "[hello, world]";

You have declared your static variable Console::prefix, but you have yet to define it. The definition goes in a single source file (the obviously candidate being console.cpp) and looks like this:

string Console::prefix;


You may want to initialise it with a default prefix:

string Console::prefix = "[hello, world]";



I now added the following lines to the Console class in console.cpp (Under "public:"):

Console() {
prefix = "[Log]";
}


Tried to build and got the following error now:

Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
"Console::prefix", referenced from:
Console::setPrefix(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in console.o
Console::log(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in console.o
ld: symbol(s) not found for architecture x86_64


Does is has to do something with the strings maybe? (I personally don't think so though)
Or does it has to be something with the way how I use the fields of the class?
It is a static variable, so it must be defined outside of any function. The lines I quoted would go, for example, here:

#include <iostream>
#include <string>
#include "console.h"

using namespace std;

string Console::prefix = "[Log]";

void Console::setPrefix(string msg) {
prefix = msg;
}

void Console::log(string msg) {
std::cout << prefix << " " << msg;
}
Thank you rip-off, that solved my issue!!

However I now have problems starting my program. It somehow does not recognizes the main function. Does it have to do something with the SDLMain.m and SDLMain.h files I need for mac applications? (Error: Undefined symbols for architecture x86_64: "_main", referenced from: start in crt1.10.6.o). My function is currently "int Main(int argc, char* argv[])", when I change "Main" to "main" it also does not work sad.png

Edit: I used Netbeans, I tried XCode insteaad and it worked! Thanks for the support :)
- can be closed now -

This topic is closed to new replies.

Advertisement