Help with linking files

I need help trying to figure out how to link my c++ files to organize my code but everything i do doesnt work. Can anyone help?
This is my code:
main.cpp:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "myFunc.h"

using namespace std;

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

myFunc.cpp:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "myFunc.h"

using namespace std;

int myFunction(){
cout << "Ran myFunction" << endl;
return 0;
}

myFunc.h:
1
2
3
4
5
#ifndef MYFUNC
#define MYFUNC
int myFunction();
#endif // MYFUNC
you don't know how to compile them ?
I do know how to compile them. i simple compile the main right? Or do i compile all 3?
g++ main.cpp myFunc.cpp -o EXECUTABLE

you may want to look at manual page for g++ for additional flags. ( like std=c++11 -Wall ...)

You don't need to compile all 3 of them because to contents of myFunc.h will be copied and pasted to main.cpp and myFunc.cpp ( C++ requires prototypes for functions)
When i compile main i get message "undefined reference to 'myFunction()' and when i compile myfunc.cpp it says 'undefined reference to winmain@16'
What IDE are you using ?
codeblocks
Would that affect it?
When i compile main i get message "undefined reference to 'myFunction()' and when i compile myfunc.cpp it says 'undefined reference to winmain@16'


You need to create a project and store all three files inside its hierarchy.

http://wiki.codeblocks.org/index.php?title=Creating_a_new_project
Ok thank you that makes much more sense. Thanks a lot.
Topic archived. No new replies allowed.