简体   繁体   中英

How to fix : 'undefined reference to foo()' with Makefile and g++

I've been trying to get into C++ again, and I have a linker error when I try to compile :

main.cpp:(.text+0x92): undefined reference to `eval(std::string, double, double, double)'

I've already tried compiling each file separatly and then linking by hand but it never works.

I have tried numerous solutions provided by other similar posts but none of these work and I don't have any idea on how to fix my problem (which doesn't seem to be that hard)

main.cpp :

#define _GLIBCXX_USE_CXX11_ABI 0

#include <iostream>
#include "functions.h"


int main()
{
    std::string expression = "1/2+x";
    double x = 1.45;
    double y = 1.65;
    double z = 1.77;

    double result = 0.0;

    result = eval(expression,x,y,z);    

    std::cout << result << std::endl;

    return 0;
}

functions.cpp :

#include "functions.h" 

double eval(std::string expression_s, double x, double y, double z)
{
        typedef exprtk::symbol_table<double> symbol_table_t;
        typedef exprtk::expression<double>     expression_t;
        typedef exprtk::parser<double>             parser_t;

        symbol_table_t symbol_table0;
        symbol_table_t symbol_table1;
    symbol_table_t symbol_table2;

        expression_t   expression;
        parser_t       parser;

        symbol_table0.add_variable("x",x);
        symbol_table1.add_variable("y",y);
    symbol_table2.add_variable("z",z);

        expression.register_symbol_table(symbol_table0);
        expression.register_symbol_table(symbol_table1);
        expression.register_symbol_table(symbol_table2);

        parser.compile(expression_s,expression);

    std::cout << expression.value();

    return expression.value();

}

functions.hpp :

ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

#include "exprtk/exprtk.hpp"
#include <iostream>

double eval(std::string expression_s, double x, double y, double z);

#endif

Makefile :

CC = g++
EXEC = Crystal_MET
LIBS = 
FLAGS = 

all: main.o
    $(CC) *.o -o $(EXEC) $(LIBS)

main.o : functions.o main.cpp functions.h
    $(CC) main.cpp -c $(FLAGS)

functions.o : functions.cpp functions.h
    $(CC) functions.cpp -c $(FLAGS)

clear :
    rm -f *.o

mr_proper :
    rm -f *.o $(EXEC)

main.cpp is compiled against a different ABI than functions.cpp because of :

#define _GLIBCXX_USE_CXX11_ABI 0

Either remove that line from main.cpp, or add it to functions.cpp .

More details : https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM