0

In my test.cpp I have:

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

int main ()
{
    auto dliste = d::data_preparation::prepare_d(100);
    cout << "Hello World!\n";
    return 0;
}

In my first.h I have:

namespace d {
    namespace data_preparation {
        something;
        std::vector<row<mdata::dliste>> prepare_d(int f);
        something;
    }
}

In my first.cpp I have:

#include "first.h"
something;
namespace d {
    namespace data_preparation {
        vector<row<mdata::dliste>> prepare_d(int f) {
            vector<row<mdata::dliste>> dliste;
            cout << f << '\n';
            return dliste;
        }
    }
}

When I compile this I get:

undefined reference to `d::data_preparation::prepare_d(int)'

EDITED

In my Makefile I have:

test: test.o
        $(CXX) -o $@ $(LDFLAGS) $^ $(LDLIBS)

Should I modify it somehow?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    Did you link first.cpp? – billz Mar 20 '13 at 08:49
  • @billz, I do not know. How can I link first.cpp? I thought that I include "first.h" and "first.h" includes "first.cpp" and, as a consequence, "first.cpp" should be visible from "test.cpp". – Roman Mar 20 '13 at 08:51
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – stijn Mar 20 '13 at 08:54
  • @Roman, add the space character between `>>` in `std::vector> prepare_d(int f);` i.e. `std::vector > prepare_d(int f);` and other similar lines. Many C++ compilers interpret `>>` in this case as operator `>>`. – megabyte1024 Mar 20 '13 at 11:16

1 Answers1

2

You have most likely forgot to link first.cpp to your executable.

Just run this commands (if you are using gcc):

g++ -c first.cpp -o first.o
g++ -c test.cpp -o test.o
g++ test.o first.o

Or just use the compact version:

g++ first.cpp test.cpp -o app

You should edit your Makefile along the lines of:

app: test.o first.o
    $(CXX) $^ -o $@ $(LDFLAGS) $(LDLIBS)

test.o: test.cpp
    $(CXX) -c test.cpp -o first.o

first.o: first.cpp
    $(CXX) -c first.cpp -o first.o

Notice: I'm forced to use 4 spaces for indentation but Makefile may require tabs instead.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • Jueecy, I use Make file. Could you please tell me how I should modify it. I put some more details to my question. – Roman Mar 20 '13 at 08:56