me1on
December 12th, 2006, 04:03 AM
I'm taking some SDL tutorials and everything is going great (at the moment I'm learning how to make a dot move around the screen :)) but I don't really like the way the code is designed. All the code is crammed into one file making it difficult to navigate, so I had the brilliant idea of spreading the code into separate files and putting all the custom SDL-related functions in it's own namespace. Problem is, I've been staring at my new code for about 3 hours and I can't figure out what's wrong with it. I managed to reproduce the error with a much smaller program, so here it is (this is C++ code by the way):
test.cpp
// test program
#include "nspace.h"
int main()
{
nspace::display();
return 0;
}
nspace.h
#ifndef NSPACE_H_
#define NSPACE_H_
#include <string>
namespace nspace
{
std::string foo = "testing";
void display();
}
#endif // NSPACE_H_
nspace.cpp
#include <iostream>
#include "nspace.h"
namespace nspace
{
void display()
{
std::cout << foo << std::endl;
}
}
Here is the compiler output:
cd '/home/john/Projects/test/debug' && WANT_AUTOCONF_2_5="1" WANT_AUTOMAKE_1_6="1" make -k
make all-recursive
Making all in src
compiling test.cpp (g++)
compiling nspace.cpp (g++)
linking test (g++)
linking test (g++)
nspace.o: In function `__tcf_1':
/home/john/Projects/test/src/nspace.h:8: multiple definition of `nspace::foo'
test.o:/home/john/Projects/test/src/nspace.h:8: first defined here
collect2: ld returned 1 exit status
make[2]: *** [test] Error 1
make[2]: Target `all' not remade because of errors.
make[2]: Nothing to be done for `all-am'.
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
*** Exited with status: 2 ***
It seems like simple enough code, but I've been trying to figure out how to fix the problem (without having to combine everything into one file) for about 3 hours now and it's really starting to annoy me. It's probably a stupid mistake, but could anyone please help me out?
Edit: Thanks Thumper, using extern solved the problem!
test.cpp
// test program
#include "nspace.h"
int main()
{
nspace::display();
return 0;
}
nspace.h
#ifndef NSPACE_H_
#define NSPACE_H_
#include <string>
namespace nspace
{
std::string foo = "testing";
void display();
}
#endif // NSPACE_H_
nspace.cpp
#include <iostream>
#include "nspace.h"
namespace nspace
{
void display()
{
std::cout << foo << std::endl;
}
}
Here is the compiler output:
cd '/home/john/Projects/test/debug' && WANT_AUTOCONF_2_5="1" WANT_AUTOMAKE_1_6="1" make -k
make all-recursive
Making all in src
compiling test.cpp (g++)
compiling nspace.cpp (g++)
linking test (g++)
linking test (g++)
nspace.o: In function `__tcf_1':
/home/john/Projects/test/src/nspace.h:8: multiple definition of `nspace::foo'
test.o:/home/john/Projects/test/src/nspace.h:8: first defined here
collect2: ld returned 1 exit status
make[2]: *** [test] Error 1
make[2]: Target `all' not remade because of errors.
make[2]: Nothing to be done for `all-am'.
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
*** Exited with status: 2 ***
It seems like simple enough code, but I've been trying to figure out how to fix the problem (without having to combine everything into one file) for about 3 hours now and it's really starting to annoy me. It's probably a stupid mistake, but could anyone please help me out?
Edit: Thanks Thumper, using extern solved the problem!