i have put off using multiple files for way too long but i get stuck here, why?

so i have a function in bunny.cpp, its prototype in the header and i call it in main like so...

here is main.cpp
1
2
3
4
5
6
7
8
#include "iostream"
#include "saystuff.h"

int main ()
{
    saystuff ();
    return 0;
}


this is saystuff.h void saystuff ();

andbunny.cpp

1
2
3
4
5
6
7
8
#include <string>
#include <iostream>
#include "saystuff.h"

void saystuff ()
{
    std::cout << "my old mans a dustman";
}


the error is undefined reference to say stuff

I never bothered to learn this while practising but now im making games with sdl it would be nice to build myself a whole library of functions so i could just slot a game together, i watch the online tutorials and i think there is always something they say that i miss.
Last edited on
First the compiler has to compile all the source files separately and then the linker links everything together into a program. The error you have is a linker error.

If you use an IDE make sure that all the files have been added to the project.

If you compile from the command line it will depend on what compiler you use. Using GCC it will look something like this
1
2
3
g++ -c main.cpp
g++ -c bunny.cpp
g++ -o nameOfExecutableFile main.o bunny.o

You can also do compilation and linking in the same line.
g++ -o nameOfExecutableFile main.cpp bunny.cpp
Last edited on
This is why i always get stuck, i never know how to tell my linker to find the .os

is there any idiot proof guides on how linker works??

i could tell my liker where to look but never learnt how to tell it to load specific files
Last edited on
how do you tell your linker to do said thing
Topic archived. No new replies allowed.