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).
Basically linking or including is done by #include<filename> or #include”filename” in C++. Now, as per your question, two c++ files can be linked by using #include”filename. cpp” , if all the files are in same directory. Otherwise specify the directory name before the filename.
Cpp files don't always have to have a header file associated with it but it usually does as the header file acts like a bridge between cpp files so each cpp file can use code from another cpp file. One thing that should be strongly enforced is the no use of code within a header file!
You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.
Visual C++ For creating more code files to go into a project, use the "Add New Item" under the "Project" menu to add new C++ code files. An executable can consist of many files, but can have only one main() function!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With