2012-11-19 80 views
0

我想編寫一個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); 
}; 

回答

3

的錯誤看起來是這裏

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

我還包括標題的內容。 –

1

製作已經知道如何構建對象文件出來適當的來源。因此,大部分的時間,你只需要定義的依賴關係,並可以簡化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 

請記住,這些命令必須由製表符作爲前綴。 而不是插入空格。

相關問題