Undefined References

I am just starting to use SFML to develop a simple game. The first part of it involves a little bit of initialization and then it starts to display credits. When I try to compile it to test it, I get an error from ld that states:

obj\Release\main.o:main.cpp: (.text+0x29b): undefined reference to 'credits(sf::RenderWindow&)'
collect2: ld returned 1 exit status

If you need the code:

main.h source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED

#include <iostream>

#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>

int init();
void credits(sf::RenderWindow& rw);

sf::Font ubuntu_regular;
sf::Font ubuntu_light;

using namespace sf;
#endif // MAIN_H_INCLUDED 


main.cpp source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "main.h"

int main()
{
    if(init() != 0)
    {
        return -1;
    }
    
    RenderWindow rw(VideoMode(800, 600, 32), "Codex");
    rw.Display();

    credits(rw);

    return 0;
}


credits.cpp source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include "commonfn.h"
using namespace sf;

void credits(sf::RenderWindow& rw)
{
    String name("myname", ubuntu_regular, 12);
    centerString(rw, name);
    fadeInString(rw, name, 3);

    return;
}


If you need more info, just tell me. If you could also point out any problems in my coding style, I would be grateful.

Thanks in advance.
Last edited on
It looks like file credits.cpp was not added to the project.
I have it added into the C::B IDE project. I did some research and I found that it might have something to do with the Makefile, but I don't know what.
Problem is your class, in main.h make sure you write
#include "credits.cpp" so you can obtain the functions inside the file.
closed account (S6k9GNh0)
No... you create either a header or implicitly resolve the reference by linking (where the latter is illegal in C++ since everything needs an explicit declaration), you don't include implementation directly. It's hard on parsing and bad design.
Last edited on
6
7
8
using namespace sf;

void credits(sf::RenderWindow& rw)
This scares me. Are you sure there isn't somehow for some reason an ::sf::sf::RenderWindow, which does not match ::sf::RednerWindow ?
OK, so by including the actual .cpp file into the main.h file, the program compiles. Is that really the actual way to do this?
No, as computerquip already pointed, you need to link the object files (the *.o)

You will end doing something like
$ g++ -c main.cpp -o main.o #compiling
$ g++ -c credits.cpp -o credits.o
$ g++ main.o credits.o -lsfml-graphics -lsfml-window -lsfml-system -o program.bin #linking


So check out how to configure your project.
With a makefile it will be something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cxx = g++
cppflags = -Wall -Wextra -pedantic-errors -O2
libraries = $(addprefix -lsfml-,graphics window system)
project = program.bin

objects = main.o credits.o

$(project) : $(objects)
	$(cxx) $(libraries) $(objects) -o $@

#dependencies
main.o: main.h
credits.o: commonfn.h

#compilation
%.o : %.cpp
	$(cxx) $< -c $(cppflags) -o $@

.PHONY: clean

clean:
	-rm $(objects) $(project)


PS: I would appreciate some comments about that makefile.
I have found a solution to the issue, when I go to my project properties, I find a list of all of my files with check boxes next to them. The box is titled build targets. My guess is that it wasn't including the files in the project build even though I imported them into the project. After checking the unchecked files, it compiles properly. I don't like the simplicity of the solution, so no doubt I will be learning how to deal with the makefile for future references. Thanks everyone.
Topic archived. No new replies allowed.