编译期间未定义的引用

问题描述:

我想编写一个make文件,但我退出了新手。我有主要文件,其中包括我写的l_mpc.hhelper.h,我也使用gnuplot,因为这个我需要gnuplot_i.hpp。编译期间未定义的引用

这是我make文件

CPPFLAGS=-I /usr/local/include/eigen3 

dc_motor_main.out : dc_motor_main.o 
    g++ -o main.out dc_motor_main.o 

dc_motor_main.o: l_mpc.o helper.o 
    g++ $(CPPFLAGS) -c dc_motor_main.cpp l_mpc.o helper.o 

gnuplot_i.o: gnuplot_i.hpp 
    g++ $(CPPFLAGS) -c gnuplot_i.hpp 

l_mpc.o: l_mpc.cpp l_mpc.h 
    g++ $(CPPFLAGS) -c l_mpc.cpp 

helper.o: helper.cpp helper.h 
    g++ $(CPPFLAGS) -c helper.cpp 

clean: 
    rm *.o dc_motor_main.out 

和输出如下:

g++ -I /usr/local/include/eigen3 -c l_mpc.cpp 
g++ -I /usr/local/include/eigen3 -c helper.cpp 
g++ -I /usr/local/include/eigen3 -c dc_motor_main.cpp l_mpc.o helper.o 
g++: warning: l_mpc.o: linker input file unused because linking not done 
g++: warning: helper.o: linker input file unused because linking not done 
g++ -o main.out dc_motor_main.o 
dc_motor_main.o: In function `main': 
dc_motor_main.cpp:(.text+0x3ab3): undefined reference to `SysMat::SysMat()' 
dc_motor_main.cpp:(.text+0x40fa): undefined reference to `SysMat::calcMPCFi(int)' 

SysMat::SysMat()是在l_mpc.h,我在哪里犯这样的错误?

这是我的头文件: 的main.cpp

#include <iostream> 
#include <Eigen/Dense> 
#include <sys/time.h> 

#include "gnuplot_i.hpp" 
#include "l_mpc.h" 
#include "helper.h" 

#define DEBUG 1 

int main(int argc, char* argv[]) 
{ .... 

helper.h

#include <iostream> 
#include <Eigen/Dense> 
#include <sys/time.h> 
#include "gnuplot_i.hpp" 

using namespace Eigen; 

double now(); 
void plot_x(MatrixXd, Gnuplot *); 
void plot_x(MatrixXd, float, Gnuplot *); 
void plot_xy(MatrixXd, MatrixXd, Gnuplot *); 
void plot_xy(MatrixXd, Gnuplot *); 

template <typename T> int sgn(T val) { 
    return (T(0) < val) - (val < T(0)); 
} 

l_mpc.h

#include <iostream> 
#include <Eigen/Dense> 
#include <sys/time.h> 
#include "gnuplot_i.hpp" 


using namespace Eigen; 

class SysMat 
{ 
    public: 
     MatrixXd Fi; 
     MatrixXd Ga; 
     MatrixXd C; 
     MatrixXd Er; 
    private:  
     MatrixXd MPCFi; 
     MatrixXd MPCGa; 
     MatrixXd MPCGy;  
    public: 
     SysMat(MatrixXd, MatrixXd, MatrixXd); 
     SysMat(); 
     ~SysMat(); 
     void calcMPCFi(int); 
     void calcMPCGa(int); 
     void calcMPCGy(int, int); 
     MatrixXd calcContSig(MatrixXd, MatrixXd, MatrixXd); 
     MatrixXd calcError(MatrixXd, MatrixXd, MatrixXd); 
}; 

的错误看起来是这里

dc_motor_main.out : dc_motor_main.o 
    g++ -o main.out dc_motor_main.o 

dc_motor_main.o: l_mpc.o helper.o 
    g++ $(CPPFLAGS) -c dc_motor_main.cpp l_mpc.o helper.o 

应该

main.out : dc_motor_main.o l_mpc.o helper.o 
    g++ -o main.out dc_motor_main.o l_mpc.o helper.o 

dc_motor_main.o: l_mpc.o helper.o 
    g++ $(CPPFLAGS) -c dc_motor_main.cpp 

假设你希望你的可执行文件,被称为main.out

当您使用g ++ -c选项时,您只能编译。没有-c的最后一步称为链接,它应该通过编译每个* .cpp文件将所有* .o文件链接在一起。

正如奥拉夫在他的回答中所说的那样,有很多种方法可以使这种重复性更低,但以上是基本步骤。

+0

感谢您的帮助,我收到了其他错误: 'g ++ -o main.out dc_motor_main.o l_mpc.o helper.o l_mpc.o :(.bss + 0x0):Gnuplot的多重定义: :tmpfile_num' dc_motor_main.o :(.bss + 0x0):首先在此处定义' –

+0

@iUngi无法在没有看到代码的情况下修复该问题。很可能你的头文件是错误的。由于这是一个不同的问题,请提出一个新问题。 – john

+0

我还包括标题的内容。 –

制作已经知道如何构建对象文件出来适当的来源。因此,大部分的时间,你只需要定义的依赖关系,并可以简化Makefile来

CPPFLAGS=-I /usr/local/include/eigen3 
LDFLAGS = # insert linker flags, if needed 
LDLIBS = # insert needed libraries here 

OBJS = \ 
dc_motor_main.o \ 
gnuplot_i.o \ 
l_mpc.o \ 
helper.o \ 

dc_motor_main.out: $(OBJS) 
    g++ $(LDFLAGS) -o [email protected] $(OBJS) $(LDLIBS) 

gnuplot_i.o: gnuplot_i.hpp 

l_mpc.o: l_mpc.h 

helper.o: helper.h 

clean: 
    rm $(OBJS) dc_motor_main.out 

请记住,这些命令必须由制表符作为前缀。 而不是插入空格。