簡體   English   中英

鏈接目標文件時g ++ / make失敗

[英]g++/make failing while linking object files

所以,我正在嘗試學習c ++和一些關於makefile的知識。 但是,出於某種原因,我無法正確鏈接我的文件。 makefile如下:

OBJS = stl_test.o src/t_stack.o src/matrix_w.o src/matrix.o

stl_test: $(OBJS) 
    g++ -o stl_test $(OBJS) -lm

.cpp.o:
    g++ -c -O -I. $< -o $@ -std=c++0x

stl_test.o: include/t_stack.h

src/matrix.o: include/matrix.h

src/matrix_w.o: include/matrix_w.h

src/t_stack.o: include/t_stack.h

include/matrix_w.h: include/matrix.h
    touch include/matrix_w.h

include/t_stack.h: include/matrix_w.h include/matrix.h
    touch include/t_stack.h

我目前遇到的問題如下:

touch include/matrix_w.h
touch include/t_stack.h
g++ -c -O -I. stl_test.cpp -o stl_test.o -std=c++0x
g++ -c -O -I. src/t_stack.cpp -o src/t_stack.o -std=c++0x
g++ -c -O -I. src/matrix_w.cpp -o src/matrix_w.o -std=c++0x
g++ -c -O -I. src/matrix.cpp -o src/matrix.o -std=c++0x
g++ -o stl_test stl_test.o src/t_stack.o src/matrix_w.o src/matrix.o -lm
src/t_stack.o: In function `T_stack::pop()':
t_stack.cpp:(.text+0xc9): undefined reference to `Matrix<double>::display() const'
t_stack.cpp:(.text+0xd7): undefined reference to `Matrix<double>::~Matrix()'
src/t_stack.o: In function `T_stack::push_translation(double, double, double)':
t_stack.cpp:(.text+0x1af): undefined reference to `Matrix<double>::Matrix(int, int, double*)'
t_stack.cpp:(.text+0x239): undefined reference to `Matrix<double>::multiply(Matrix<double>*) const'
src/t_stack.o: In function `T_stack::push_scaling(double, double, double)':
t_stack.cpp:(.text+0x33f): undefined reference to `Matrix<double>::Matrix(int, int, double*)'
t_stack.cpp:(.text+0x3c9): undefined reference to `Matrix<double>::multiply(Matrix<double>*) const'
src/t_stack.o: In function `T_stack::T_stack()':
t_stack.cpp:(.text+0x4f4): undefined reference to `Matrix<double>::Matrix(int, int, double*)'
t_stack.cpp:(.text+0x516): undefined reference to `Matrix<double>::copy() const'
src/t_stack.o: In function `T_stack::~T_stack()':
t_stack.cpp:(.text+0x64c): undefined reference to `Matrix<double>::display() const'
t_stack.cpp:(.text+0x65a): undefined reference to `Matrix<double>::~Matrix()'
t_stack.cpp:(.text+0x74f): undefined reference to `Matrix<double>::display() const'
collect2: ld returned 1 exit status
make: *** [stl_test] Error 1

我不完全確定問題是什么。 我嘗試在g ++命令中更改文件的順序但是也沒有用。 我已經嘗試檢查文件的標題,我認為它們是正確的但我在這里發布了片段:

stltest.cpp

#include <iostream>

#include "include/t_stack.h"

using namespace std;

int main(void) {
......

matrix.h

#ifndef _MATRIX_
#define _MATRIX_

#include <iostream>

using namespace std;
......

matrix_w.h

#ifndef _MATRIX_W_
#define _MATRIX_W_

#include "include/matrix.h"

class Matrix_w {
......

t_stack.h

#ifndef _T_STACK_H_
#define _T_STACK_H_

#include <list>
#include <stack>
#include <iostream>
#include "include/matrix.h"
#include "include/matrix_w.h"

using namespace std;

class T_stack {
    private:
        // matrix stack
        stack<Matrix_w*, list<Matrix_w* > >* m_stack;
        // inverse transform list
        list<Matrix<double>* >* t_list;
    public:
        T_stack();
        void pop();
        void push_translation(double tx, double ty, double tz);
        void push_scaling(double sx, double sy, double sz);
        int size() const;
        ~T_stack();
};

#endif

任何想法可能是什么問題? 謝謝!

以防萬一,這里是它所抱怨的方法的實現(該文件名為matrix.cpp)。

#include "include/matrix.h"

using namespace std;

template <typename T>
Matrix<T>::~Matrix() {
    delete data;
}

template <typename T>
Matrix<T>::Matrix(int width, int height) {
    this->height = height;
    this->width = width;
    this->data = new T[height*width];
}

template <typename T>
Matrix<T>::Matrix(int width, int height, T* data) {
    this->height = height;
    this->width = width;
    this->data = new T[height*width];

    int i;
    //may be able to speed this up by using memcpy
    // Not sure whether this is a good idea anyways
    for(i=0; i < height*width; i++) {
        this->data[i] = data[i];
    }
}

template <typename T>
T Matrix<T>::at(int x, int y) const {
    #ifdef __DEBUG
        if(x < width && y < height) {
            return data[y*width + x];
        } else {
            throw 1;
        }
    #else
        return data[y*width + x];
    #endif
}

template <typename T>
void Matrix<T>::set(int x, int y, T val) {
    #ifdef __DEBUG
        if(x < width && y < height) {
            data[y*width + x] = val;
        } else {
            throw 1;
        }
    #else
        data[y*width + x] = val;
    #endif
}

//this function is just for convenience but it should only work
//when T is some number type ----ASK!!
template <typename T>
void Matrix<T>::display() const {
    int i, j;

    cout << "[" << endl;
    for(i=0; i<height; i++) {
        for(j=0; j<width; j++) {
            cout << " " << data[i*width + j];
        }
        cout << endl;
    }
    cout << "]" << endl;
}

template <typename T>
Matrix<T>* Matrix<T>::multiply(Matrix<T>* other) const {
    #ifdef __DEBUG
        if(other->height != width) {
            throw 1;
        }
    #endif

    T* res = new T[other->width*height];
    int i,j,k;
    T sum;

    for(i=0; i<height; i++) {
        for(j=0; j<other->width; j++) {
            sum = 0;
            for(k=0; k<width; k++) {
                sum += other->data[k*other->width+j]*data[i*width+k];
            }
            res[i*other->width + j] = sum;
        }
    }

    return new Matrix<double>(other->width, height, res);
}

template <typename T>
Matrix<T>* Matrix<T>::copy() const {
    return new Matrix<T>(width, height, data);
}

當你有一個模板類或函數時,函數應該是內聯的,並且正文應該在頭文件中,在類中或在類外面使用inline關鍵字。

不同之處在於,與常規類不同,編譯器正在為您用作模板類型的每種類型編譯類。

如果您根本不使用它,則文件將不會被編譯一次。

在幾個編譯器中,即使將代碼拆分為h和cpp,代碼也能正常工作。 但你不能指望它。

我知道,將模板實現為內聯函數通常是典型的。 但是,您可以在matrix.cpp文件中使用模板的顯式實例化。 在底部,在定義之后,您可以嘗試添加:

   template class Matrix<double>;

如果你沒有使用預編譯的頭文件,這實際上可能很有意義。 此外,它可以在我想的任何地方進行,但是每個程序只能有一個這樣的語句,如果你在兩個地方實例化一個矩陣,它可能會也可能不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM