Sign in to follow this  

Problems with linking files (Undefined symbols for architecture x86_64)

This topic is 1967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
[CODE]
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
[/CODE]

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)
[CODE]
#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;
}
[/CODE]

console.h
[CODE]
#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
[/CODE]

console.cpp
[CODE]
#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;
}
[/CODE]


(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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:
[code]
string Console::prefix;
[/code]

You may want to initialise it with a default prefix:
[code]
string Console::prefix = "[hello, world]";
[/code]

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1343215586' post='4962899']
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:
[code]
string Console::prefix;
[/code]

You may want to initialise it with a default prefix:
[code]
string Console::prefix = "[hello, world]";
[/code]
[/quote]

I now added the following lines to the Console class in console.cpp (Under "public:"):
[CODE]
Console() {
prefix = "[Log]";
}
[/CODE]

Tried to build and got the following error now:
[CODE]
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
[/CODE]

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

Share this post


Link to post
Share on other sites
It is a static variable, so it must be defined outside of any function. The lines I quoted would go, for example, here:
[code]
#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;
}
[/code] Edited by rip-off

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

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

Share this post


Link to post
Share on other sites

This topic is 1967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this