开发者

C++ Project Layout seems incorrect, compiler can't find implementation files

I'm writing a C++ Package for later use using Code::Blocks.

The project structure looks like this:

cNormal\
    cNormal.cdp
    src\
        main.cpp            # for testing purpose
        cnormal_defs.h      # important compiler definitions
        cnormal_storage.h   # includes all .h files from "./storage"
        storage\
            cnarray.h
            cnarray.cpp
            cnstack.h
            cnstack.cpp
    bin\
    obj\

The cnormal_storage.h:

// cnorm开发者_开发问答al_storage.h
// *****************************************************************
// Includes all necessary headers for the cNormal storage subpackge.
//

#ifndef _cNORMAL_STORAGE_H_
#define _cNORMAL_STORAGE_H_

    #include "storage/cnarray.h"
    #include "storage/cnstack.h"

#endif // _cNORMAL_STORAGE_H_

To test the classes, i create a main-function in main.cpp.

// main.cpp
// *****************************************************************
// The main-file.
//
#include <iostream>
#include "cnormal_storage.h"

using namespace std;

int main() {
    cnArry<int> arr(10);
    arr[9] = 999;
    arr[0] = 0;
    cout << arr[9] << endl;
    cout << arr.getLength();
}

But the compiler (gcc) gives me undefined reference to ... errors about cnArray.

Now, the cnarray.cpp includes cnarray.h (as it is the implementation file), so using

#include "storage/cnarray.cpp" works just fine.

It seems like the compiler can't find the implementation of cnarray.h which is located in cnarray.cpp.

I assume it's because of the folder-structure, can you tell me how I can fix this ?

Even adding src\storage to the include directives does not fix it. (And I also don't want to add it to the include-paths as this would be very unhandy for a package.)


Can you post the compilation command that you use?

Seems like you are compiling only main.cpp and not compiling (and thus linking) the other .cpp files.


I could spot the error now, cnArray.h declared a template class and template classes cannot be implmented in another file than they are declared, because the compiler must know about the implementation when compiling, not when linking.

I have found a workaround on the internet to #include the implementation in the headerfile, but exclude the implementation file from compiling. This works just fine now.

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜