codeblocks/g++ problem compiling module [SOLVED]

Hello, I'm trying to learn how to do basic modules in c++ but seems impossible to me. i followed some books but at last always the compiler can't reach the functions in the module and it's frustrating, please if somebody can explain me what i'm doing bad I will be greatful.

(My Ide is codeblocks and I use gcc-4.7.3-r1)

following some books, first I declare the interface cab.h
interface

Código:


/* cab.h

Interface file */

void recogeValores();



Second, I build the implementation "cab.cpp"
Código:

/* cab.cpp
implement functions declared on interface */

#include "cab.h" //<--------------- Including the interface
#include <iostream>

using namespace std;

void recogeValores()
{
cout << "Se ejecutó recoger valores\n";
}



And for end, i make the main program

Código:

/* main.cpp */

#include "cab.h" //<--------------- Including the interface
#include <iostream>


using namespace std;

int main (){
recogeValores();
}



According to the books all will be fine. i did include the calls to the interface on main and implementation, however when i try to compile, g++ says: reference to recogeValores() undefined

On the other hand, if I delete the #include "cab.h" on the implementation, and add #include "cab.cpp" at the begining of the interface file, the program compile and works, but I think that this isn't the correct way to write the programs.

Please can anybody show me the correct way to write the program and solve the issue?
Thans in advance

(Sorry by my limited English)
Last edited on
What command line are you using with g++ to compile the program? It should include all source files, not just main:

g++ main.cpp cab.cpp -o myprogram
Wow, that worked perfect on bash! But then why don't over codeblocks?

Thank you very much for all!
> however when i try to compile, g++ says: reference to recogeValores() undefined
http://www.cplusplus.com/forum/general/113904/#msg622055
Project -> Add files

> and add #include "cab.cpp" at the begining of the interface file,
> the program compile and works, but I think that this isn't the correct way to write the programs.
http://www.cplusplus.com/forum/beginner/34484/#msg186401


g++ main.o cab.o -o program.bin
Solved!

The problem was mine! Sorry, I forgot put a tick on Debug & Realase inside "add file to active project on build targets" for the implementation and declaration files

thank you for all.
Topic archived. No new replies allowed.