Need another set of eyes

Hi,

I have been running through c++ online coding classes as fast as I can trying to get myself up to speed but I seem to have run into an issue when I try to call my functions from separate sources. As far as I can tell I am not missing anything but I get the same error every time I try to compile and run the programs (3 seperate attempts with mini programs thus far). I have seen this question asked several times on these forums and have gone over them but I am not seeing where I am screwing this up.

I included on example below I'm not certain if it matters but I am also using codeblocks w/ mingw

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//main.cpp

#include <iostream>
#include "volume.h"

using namespace std;

int main()
{
    double depth, width, length;
    cout << "How deep?" << endl;
    cin >> depth;
    cout << "How wide?" << endl;
    cin >> width;
    cout <<"How long?" << endl;
    cin >> length;

    cout << "The total volume is  " << CalculateVolume(depth, width, length) << endl;

    return 0;
}


1
2
3
4
5
6
7
8
//volume.h

#ifndef VOLUME_H_INCLUDED
#define VOLUME_H_INCLUDED

double CalculateVolume(double depth, double width, double length);

#endif 


1
2
3
4
5
6
7
8
//volume.cpp

#include "volume.h"

double CalculateVolume(double depth, double width, double length)
{
    return depth*width*length*1000;
}



*EDIT
The error I am getting applies to line 18 of main.cpp and says
"undefined reference to CalculateVolume(double, double, double)"
Last edited on
Linker error? Linker needs both object files.
I'm sorry could you give an example, I assume you are referring to the .h file?
Compiler creates an object file from each *.cpp. Linker creates the executable from the object files.

With GCC one can do both steps with oneliner:
g++ -Wall -Wextra -o foo main.cpp volume.cpp
does not codeblock automatically manage your build ? Eclipse does :P
The compiler error you have is because you compile only the main.cpp unit.
And cannot find a definition of CalculateVolume(double, double, double) within any .cpp files you have compiled with.

This is because you (extremely rare case: or the compiler) have forgotten to compile WITH volume.cpp file.

Otherwise this compiling error would not show up.

PS: Tested on my machine with g++. If the Error still shows up, please fill in the following info so we can all be aware of the details:
Compiler: <Name, Version>
Command to compile: <command> <flags> <source files> <options>
Additional Details: <architecture of your files> (example: headers directory path, source directory path)
Last edited on
does not codeblock automatically manage your build ? Eclipse does :P


I'm not to sure I know what you are asking I assume you meant that Eclipse takes your source files and automatically links them. If that is the case no codeblocks does not appear to do this.

I apologize for my lack of understanding I am very new to coding and I am sort of just playing around with it to see what I can do.

keskiverto I assume you are referring to this when your talking about GCC?

http://gcc.gnu.org/
gcc = g++

if you want to make your compiler understand you have multiple source files (.cpp) to use within your code... you have to specify ALL the files

Assuming you have the header file (.h, volume.h) in the same directory:
these following commands should work on a terminal.

g++ main.cpp volume.cpp

if you want to specify an executable name:
g++ -o name.exe main.cpp volume.cpp

So if I understand correctly you should be able to do that within a console.

But if you want to compile from Code::Blocks

The files should be all within the same Project Folder and be compile from the Project Folder's path. :)

If you still have troubles you can search in these following links, (I'm not a Code::blocks user :P)
http://www.codeblocks.org/user-manual
http://wiki.codeblocks.org/index.php?title=FAQ
Last edited on
It does, but he isn't doing a project. He has C::B open with just three individual files and main.cpp highlighted so it can't find it. For C::B to do it automatically you have to do File -> New Project -> Empty Project fill out the dialogs that pop up and add the three files to that project. Then it will compile with no errors.
I was just going to say that after digging through user manual a bit I figured out more what you meant by managed projects.

In code blocks under my build options I realized that I did not have all 3 files set to build. (only main.cpp)

After clicking a stupid check box and about 2 hours of my life I feel more a scrub now then I did before I asked the question I appreciate all the help you guys put me on the right track.

should anyone else run into this problem here is how to fix it

Project > Properties > Build targets

At the bottom center you will find all your build target files make sure they are selected!

words do not describe.....
I love the old way of doing so: Makefile FTW!
Makefiles is a good thing to learn as well as CMake and Scons. Some teams I have worked with swear by CMake and Scons while I know that some companies still prefer Makefiles. Think it was id Software I read about who had a system in place so that at like 6am every morning Doom3 would compile so they could start debugging it as soon as they got in.
Last edited on
Different concepts and approaches.

Calling compiler directly is very "low-level".

Makefile is a list of rules for how 'make' should call the compiler for you. Essentially a "project".

CMake, qmake, GNU autotools, SCons are tools that read their own rules in order to generate a Makefile (and then build). Portable project, so Makefile will have different rules on different platforms.

Code::blocks, Eclipse, Visual Studio are IDEs. Integrated Development Environments. They provide GUI for defining the project rules and execute (in background) whatever automake/make/compiler toolchain they are set to use.
Topic archived. No new replies allowed.