c++ - 编译期间 undefined reference

标签 c++ makefile

我想写一个 make 文件,但我是新手。我有主文件,其中包含我编写的 l_mpc.h helper.h,而且我正在使用 gnuplot,因此我需要 gnuplot_i.hpp .

这是我的文件

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[])
{   ....

助手.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 文件链接在一起。

正如 Olaf 在他的回答中所说,有多种方法可以减少重复,但以上是基本步骤,无论您如何操作。

关于c++ - 编译期间 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13458339/

相关文章:

c++ - “lock cmpxchg”如何在汇编中工作?

c++ - 使用 "Stream-like"输入到 C++ 函数

linux - 在编译启用 fips 的 openssl 时使用 make depend

c++ - 连接多个字符

c++ - 在 C++ 中将 md5 字符串转换为 base 62 字符串

c++ - 模板接口(interface)实现类 C++ 错误

C - Makefile 可能缺少一行

c++ - 将具有外部实现的模板编译为静态库

xcode - 奇怪的错误消息: xcode-select: error: malformed developer path ("/Library/Developer/CommandLineTools")

python - 如何为 python 脚本更新数据库编写 makefile?