gpt4 book ai didi

c++ - g++/make 链接目标文件时失败

转载 作者:行者123 更新时间:2023-11-28 02:53:52 24 4
gpt4 key购买 nike

所以,我正在尝试学习 C++ 和一些关于 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) {
......

矩阵.h

#ifndef _MATRIX_
#define _MATRIX_

#include <iostream>

using namespace std;
......

矩阵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,您的代码也能正常工作。但你不能指望它。

关于c++ - g++/make 链接目标文件时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22444890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com