Undefined reference to a C function in C++

Hello,
I am facing this problem and i don't know what to do now, i am trying to use a C library to parse a math expression stored in a string i included the header file and source dile so when i use the functions i am getting this error, you will say that there are a lot of solutions for it on net, I know but no one has solved my problem i tried a lot of solutions but the same error,
This is my file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "window.hpp"
#include "include/tinyexpr.h"

  void main_window::on_result_button_clicked(){

    Glib::ustring string1 = main_entry.get_text();
   
   char chars_array[string1.length()];

   for (int i = 0; i < string1.size(); i++)
   {
      chars_array[i] = string1[i];
   }

   te_expr * exp = te_compile(chars_array,0,0,0);

   double result = te_eval(exp);
   
   main_entry.set_text(Glib::ustring::format(result))
   
   te_free(exp);
 }


And this is the library that i use it's really small so you can read its source code :
The header file:
https://github.com/codeplea/tinyexpr/blob/master/tinyexpr.h

And this is the source code :
https://github.com/codeplea/tinyexpr/blob/master/tinyexpr.c

The command that i use to compile :
g++ main.cpp operations.cpp -o MCalc
And this is the error:
1
2
3
4
5
6
7
/usr/bin/ld: /tmp/ccu4lnbQ.o: in function 'main_window::on_result_button_clicked()' 
operations.cpp: (.text+0x915): undefined reference to 'te_compile' 
/usr/bin/ld: operations.cpp: (.text+0x976): undefined reference to 'te_free'

/usr/bin/ld: operations.cpp: (.text+0x920): undefined reference to 'te_eval'

collect2: error: ld returned 1 exit status


If anyone could help me i would be really happy,
Last edited on
Header files (.h) usually declare the public API of a library, e.g. function prototypes, classes, etc. By #include'ing the header file, you make the API available in your source file, so that you can use it. Still, the library itself, i.e. the implementation, needs to be linked to your program! If you don't actually link the library, then you are trying to call functions (or use classes) that don't exist in your program, or any of the linked libraries. Hence you'll get linker errors ("undefined reference"). The solution is to link the required library ;-)

For example:
g++ main.cpp operations.cpp -lfoobar -o MCalc

(the above command will link the hypothetical library "libfoobar.a". Replace it with the actual library name!)


BTW: The library should either come with a pre-compiled library (.a) file, or there should be a Makefile to build the required library file from the source codes. See the docs that come with the library for details!
Last edited on
Mamo Grag wrote:
The command that i use to compile :
g++ main.cpp operations.cpp -o MCalc



As a side issue, always compile with warnings turned on, and compile against a c++ standard.

g++ -std=c++20 -Wall -Wextra -pedantic main.cpp operations.cpp -o MCalc


add the library as per kigar64551 post above.

With the standard, one may get away with out it, if one has a later compiler version. g++ these days defaults to c++17, older ones do c++14, or c++98.

Warnings are your friend, they tell what is wrong with the program. Even though the program may run, there are likely run time errors/ problems coming from the warnings mentioned.

It's worth reading the manual, there are a zillion options, but still a number of useful ones that aren't turned on with -Wall and -Wextra

Another thing is to compile with libstdc++ if using anything from the c++ library features.
Topic archived. No new replies allowed.