-1

I have 1 cpp file with main(). It relies on structs and functions in another (say, header.hpp). The structs are defined in header.hpp, along with function prototypes. The functions are implemented in header.cpp.

When I try to compile, I get an error message saying:

undefined reference to `see_blah(my_thing *)`

So to give an overview:

header.hpp:

#ifndef HEADERDUR_HPP
#define HEADERDUR_HPP
struct my_thing{
    int blah;
};
int see_blah(my_thing*);
#endif

header.cpp:

#include "header.hpp"
int see_blah(my_thing * thingy){
    // ...
}

main.cpp:

#include <iostream>
#include "header.hpp"
using namespace std;
int main(void)
{
    thinger.blah = 123;
    cout << see_blah(&thinger) << endl;
    return 0;
}

I have no idea what I'm doing wrong, and I can't find any answers. Thanks for any answers, they are very much appreciated!

K-RAN
  • 896
  • 1
  • 13
  • 26
  • Are you compiling both of your source files ? – Mahesh Feb 07 '11 at 07:56
  • you mean compile each of the cpp files into .o files and then linking them? – K-RAN Feb 07 '11 at 08:02
  • if the accepted answer fixed your problem, then your question wasn't very well formulated. Your code will not compile as is (with at least two compile time errors). Not being bitter or anything :-) It's just questions on SO are not that useful unless they're accurate so my only request is that you take more care in future. Can we assume here that you actually _have_ a semi-colon at the end of your structure definition? – paxdiablo Feb 07 '11 at 08:17

6 Answers6

4

You should be aware that you're missing a semi-colon at the end of your structure definition. This means it's folding the two (supposedly separate) parts together and that you're not getting the function prototype as a result.

The following compiles fine (after fixing a couple of other errors as well):

// main.cpp
#include <iostream>
#include "header.hpp"
using namespace std;            // <- not best practice, but irrelevant here :-)
int main(void)
{
    my_thing thinger;           // <- need this!
    thinger.blah = 123;
    cout << see_blah(&thinger) << endl;
    return 0;
}

// header.cpp
#include "header.hpp"
int see_blah(my_thing * thingy){
    // ...
}

// header.hpp
#ifndef HEADERDUR_HPP
#define HEADERDUR_HPP 
struct my_thing{
    int blah;
};                              // <- see here.
int see_blah(my_thing*);
#endif

with:

g++ -o progname main.cpp header.cpp

gcc actually gave an error with that code you posted so I'm not certain why your compiler didn't. That command line above is also important - if you're compiling and linking in one step, you need to provide all required C++ source files (otherwise the linker won't have access to everything).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • what does 'qq' in that line do? – K-RAN Feb 07 '11 at 08:03
  • @K-RAN, that's my standard "temporary file" since it's easy to type. The `-o qq` means write the output to the file `qq`. I suppose it would be easier to leave it off altogether and just get `a.out` but old habits die hard. Changed it to progname to hopefully avoid confusion. – paxdiablo Feb 07 '11 at 08:04
2

You need to:

#include "header.hpp"

in your *main.cpp file.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
2

Your code is fine. You're just compiling it wrong. Try:

g++ main.cpp header.cpp
TonyK
  • 16,761
  • 4
  • 37
  • 72
1

If you have included header.hpp, than probably you haven't link it(header.cpp) with main.cpp. What environment are you using(g++ or VC++)?

Edit:for linking in g++ you must write:

g++ main.cpp header.cpp -o program

Also you are missing semicolon in the end of your struct!

UmmaGumma
  • 5,633
  • 1
  • 31
  • 45
  • linux, g++. Look at the sample code again. I've checked it and it's what I have this time. – K-RAN Feb 07 '11 at 07:56
0

You are missing a semi colon at the end of your structure definition and mixing it with the method.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
0

thinger.blah = 123; should be along the lines of:

my_thing thinger = { 123 };

in addition to issues other posters have mentioned. please, update your example so it compiles.

justin
  • 104,054
  • 14
  • 179
  • 226