Linking Two .cpp and .h Files

I am doing an exercise (from the third chapter, Thinking in C ++), but I have a problem with two .cpp files.


This exercise:

Create a header file (with the extension '.h). In this file, declare a group of functions by changing the argument lists and return values ​​from the following: void, char, int and float. Now create a .cpp file that includes the header file and creates the definitions for all these functions. Each definition should simply print the function name, argument list, and return type so you know what it was called. Create a second .cpp file that includes your header file and defines int main () containing calls to all your functions. Compile and run your program.

I created three files, but when I try to compile the compiler give me this error:

undefined reference to `func1 () '

undefined reference to `func2 (int) '|

undefined reference to `func3 (char, int, double) '|

undefined link to `func4 () '|

|| === Assembly completed: 4 errors, 0 warnings === |

Why can't he find the function declaration?

## EDIT
These are my three files:

func_ex.h

void func1(void);
int func2(int i);
char func3(char c, int i, double d);
float func4(void);

func_ex.cpp

#include "func_ex.h"
using namespace std;

void func1(void) {
    cout << "void func1(void)" << endl;
}

int func2(int i) {
    cout << "int func2(int i)" << endl;
}

char func3(char c, int i, double d) {
    cout << "func3(char c, int i, double d)" << endl;
}

float func4(void) {
    cout << "func4(void)" << endl;
}

func_ex_main.cpp

#include "func_ex.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    func1();
    int i;
    func2(i);
    char c; double d;
    func3(c, i, d);
    func4();
}

I use GCC as a compiler (and Code :: Blocks as an IDE, but I think it doesn't matter).

+5
source share
7 answers

It seems that the file does not find the corresponding functions. Is the header file included in both files? You can enable it as:

#include "myheader.h"

Have you forgotten to compile both files together? For instance:

gcc -o myprogram file1.cpp file2.cpp

+8
gcc -c func_ex.cpp -o func_ex.o
gcc func_ex_main.cpp func_ex.o -o func_ex_main
+6

, , IDE BloodShed Dev ++.

, H CPP .

, , , , , H CPP. , , .

, Bloodshed.

http://www.horstmann.com/bigcpp/help/bloodshed/index.html

IDE, , . - , .

0

code:: blocks, . IDE . : project β†’ properties β†’ build target β†’ , . , !

0

, func1(), func2(), func3() . (#include guard):

#ifndef func_ex.h
#define func_ex.h
/* your code */

#endif 
0

, #include"func_x.cpp" func_main.cpp

-1

include :

#include "headerfilename.h"

at the very top of each .cpp document.

-3
source

All Articles