سؤال

My project is structured like this:

ProjName/
    | configure.ac
    | Makefile.am
    | src/
        | Main.cpp
        | Main.h
        | Extra.cpp
        | Extra.h

Both Main and Extra are classes within the same namespace MyNamespace. Even though Main.h includes Extra.h I'm getting a linker error:

in function MyNamespace::Main::Main():
undefined reference to MyNamespace::Extra::Extra()

I've made sure that all class are encased in

namespace MyNamespace { ... }

as well as adding using namespace MyNamespace to Main.cpp and the constructors for both classes are correct.

The only thing that seems to solve this problem is adding #include "Extra.cpp" to Main.cpp but this would just defeat the purpose of using a header file at all, and will get extremely tedious when I add more files to the project.

Edit:

Makefile.am looks like:

bin_PROGRAMS= ProjName

ProjName_CPPFLAGS= -I$(top_srcdir) -I$(top_srcdir)/src
ProjName_SOURCES= src/Main.cpp Main.h

My project uses the Ogre3D library so there are also some Ogre specific things. I've tried adding the cpp and header file to ProjName_SOURCES and running ./bootstrap && ./configure && make but I then get an error, something like:

No rule to make .deps/src/ProjName-Extra.Po

Or something along those lines (I'm on the phone at the moment).

هل كانت مفيدة؟

المحلول

You have not specified the source files correctly. Try:

ProjName_SOURCES = src/Main.cpp src/Extra.cpp

نصائح أخرى

This means Extra.cpp is not compiled and linked to your executable. Check your makefile.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top