0

在我的test.cpp我有:

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

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

在我的first.h我有:

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

在我的first.cpp我有:

#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;
        }
    }
}

当我编译这个时,我得到:

未定义对 `d::data_preparation::prepare_d(int)' 的引用

已编辑

在我的Makefile我有:

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

我应该以某种方式修改它吗?

4

1 回答 1

2

您很可能忘记链接first.cpp到您的可执行文件。

只需运行以下命令(如果您使用的是 gcc):

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

或者只使用紧凑版本:

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

您应该按照以下方式编辑您的 Makefile:

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

注意:我被迫使用 4 个空格进行缩进,但 Makefile 可能需要制表符。

于 2013-03-20T08:52:08.867 回答