• Create Account

Problems with linking files (Undefined symbols for architecture x86_64)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

#1SpaceEmotion  Members

119
Like
0Likes
Like

Posted 25 July 2012 - 03:26 AM

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


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.)

Edited by SpaceEmotion, 25 July 2012 - 03:34 AM.

#2 abcdef44   Banned

2
Like
0Likes
Like

Posted 25 July 2012 - 05:11 AM

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.

#3rip-off  Moderators

10730
Like
1Likes
Like

Posted 25 July 2012 - 05:26 AM

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]";


#4SpaceEmotion  Members

119
Like
0Likes
Like

Posted 25 July 2012 - 06:16 AM

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


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?

Edited by SpaceEmotion, 25 July 2012 - 06:32 AM.

#5rip-off  Moderators

10730
Like
2Likes
Like

Posted 25 July 2012 - 07:00 AM

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;
}


Edited by rip-off, 25 July 2012 - 07:01 AM.

#6SpaceEmotion  Members

119
Like
0Likes
Like

Posted 25 July 2012 - 10:06 AM

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

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

Edited by SpaceEmotion, 25 July 2012 - 11:55 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.