13

I'm doing an exercise (from the third chapter of Thinking in C++) but I have a problem linking two .cpp files.

This is the exercise:

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

I've created the 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 reference to `func4()'|

||=== Build finished: 4 errors, 0 warnings ===|

Why it cannot found 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'm usig GCC as compiler (and Code::Blocks as IDE, but I think that's not important).

Community
  • 1
  • 1
Overflowh
  • 1,103
  • 6
  • 18
  • 40
  • 2
    How are you trying to compile the program? It seems like you're not compiling/linking one of the files. – templatetypedef Aug 11 '11 at 20:56
  • lets see the contents of your 3 files and your makefile (if that's what you're using) – K Mehta Aug 11 '11 at 20:57
  • Can you post your files you are using? – samoz Aug 11 '11 at 20:57
  • Have you followed exactly the instructions? Which compiler, what version? Show us some code, please. – celavek Aug 11 '11 at 20:58
  • 2
    Sounds like you are using the compiler wrong. The obvious explanation would be that you didn't tell the compiler about your first .cpp file, so it didn't find any of the function declarations in it. Post a few more details about exactly how you are using your compiler. – john Aug 11 '11 at 20:58
  • @templatetypedef I think so, but I don't know how fix this problem... I'm not using a makefile, and maybe this is the problem. – Overflowh Aug 11 '11 at 21:17
  • @unNaturhal- What are you doing to compile the files? Are you compiling from the command-line? Using Visual Studio? Xcode? – templatetypedef Aug 11 '11 at 21:18
  • @templatetypedef I'm using Code::Blocks and to compile I just click on the 'build' button. – Overflowh Aug 11 '11 at 21:19
  • 1
    @unNaturhal: Are you sure both your cpp files are included in the Code::Blocks project? – Nemanja Trifunovic Aug 11 '11 at 21:22
  • 1
    Well your code is fine (ish). You must be using your IDE wrong. Sorry but I can't help there. – john Aug 11 '11 at 21:23
  • 1
    Actually the code you posted will not compile because you didn't have `#include ` in func_ex.cpp. It is possible an error in compiling func_ex.cpp is the root cause of the problem? Might explain why the linker fails to find the functions, if the file containg the functions has failed to compile. Pretty poor IDE if that's the case though. – john Aug 11 '11 at 21:24
  • 1
    @Nemanja: No, I'm not sure. Honestly I didn't know that I had to create a projet. However now I have create it and everythink works properly! :D – Overflowh Aug 11 '11 at 21:34
  • 1
    @john: I've added the also in func_ex.cpp right now, thanks. – Overflowh Aug 11 '11 at 21:34

7 Answers7

12

It sounds like the file is not finding the functions appropriately. Is the header file included in both files? You can include it like:

#include "myheader.h"

Did you make sure to compile both files together? Such as:

gcc -o myprogram file1.cpp file2.cpp
samoz
  • 56,849
  • 55
  • 141
  • 195
8
gcc -c func_ex.cpp -o func_ex.o
gcc func_ex_main.cpp func_ex.o -o func_ex_main
xda1001
  • 2,449
  • 16
  • 18
0

For those like me who may be using the IDE BloodShed Dev C++.

Create a project folder then proceed to place your H files and your CPP files inside your project folder.

I had the same problem as mentioned above, the conclusion I came upon was that my compiler was only compiling the H files and wouldn't even read the CPP files linked to it. When I put them in a project folder my compiler would compile all files together hence allowing my code to run.

Here is a link of creating a project folder for those using Bloodshed.

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

As for other IDE users, I believe the problem to be similar. IF anyone needs further elaboration just let me know.

  • I was still having trouble doing simple linking with Dev C++ even with the instructions you linked to. However, [this discusison](http://www.cplusplus.com/forum/beginner/33274/2/) shed additional light. The key was to add a project folder, not a regular folder. to the project and then put the source files in there. This finally made Dev C++ understand that it should compile the files as in @xda1001's example. – Spencer Williams Jul 12 '21 at 00:09
  • Okay, it turns out the instructions linked to here are perfectly fine and you do not need to add a project folder. I had drag-and-dropped a file in Dev-C++ that I later added to my project, but I see now that it was first trying to build from the original file outside a project context. – Spencer Williams Jul 12 '21 at 21:11
0

In code::blocks the default setting is that only one file is built.

Here's what I did to fix this problem:

project -> properties -> build targets ->
Select files related in the bottom right section.
newbie
  • 1
-1

First of all, how can you have func1(), func2(), func3() without having a return statement. Try to put your header body inside these (#include guards) :

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

#endif 
-1

I think you should have #include"func_x.cpp" in your func_main.cpp

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
Nebiat
  • 1
  • 2
    In general, one shouldn't `#include` `.cpp`-files, they are supposed to be linked in. In the rare event that you design an implementation file that should be `#include`d, I have seen people use an `.ipp`-extension. Specifically, I have seen this in boost. – Magnus Hoff Feb 09 '12 at 10:37
-3

You need to put an include like this:

#include "headerfilename.h"

at the very top of each .cpp document.

Drew Wiens
  • 124
  • 1
  • 8