I'm trying to call a C++ function in a C program. I declared the function in a .h file as follows- data_fields.h:
#ifdef __cplusplus
extern "C"
{
#endif
char GetCharFromHexAscii(char *);
#ifdef __cplusplus
}
#endif
And I call the GetCharFromHexAscii() in my .c file as follows- GUIForRenamer.c:
#include<stdio.h>
#include<gtk/gtk.h>
#include<unistd.h>
#include<string.h>
#include<ctype.h>
#include"data_fields.h"
char * AsciiDecode(char *Path)
{
int k = 0;
char *convertedPath, *asciiChar;
convertedPath = (char *)malloc(strlen(Path) + 1 * sizeof(char));
asciiChar = (char *)malloc(3 * sizeof(char));
for(int i = 0; Path[i] != '\0'; i++)
convertedPath[k++] = GetCharFromHexAscii(asciiChar);
return convertedPath;
}
The definition of the C++ function in .cpp is as follows- LibFunc.cpp:
#include<iostream>
#include<cstdlib>
#include"data_fields.h"
char GetCharFromHexAscii(char *asciiChar)
{
int hexVal = stoi(asciiChar, 0, 16);
return (char)hexVal;
}
Here's the command I use to compile my C code-
gcc GUIForRenamer.c -lm `pkg-config --libs --cflags gtk+-3.0` -export-dynamic FileExtensionRenamer.c LibFunc.cpp
But the compilation fails with the following error-
LibFunc.cpp: In function ‘char GetCharFromHexAscii(char*)’:
LibFunc.cpp:7:15: error: ‘stoi’ was not declared in this scope
int hexVal = stoi(asciiChar, 0, 16);
^~~~
LibFunc.cpp:7:15: note: suggested alternatives:
In file included from /usr/include/c++/7/string:52:0,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from LibFunc.cpp:1:
/usr/include/c++/7/bits/basic_string.h:6477:3: note: ‘std::__cxx11::stoi’
stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
^~~~
/usr/include/c++/7/bits/basic_string.h:6477:3: note: ‘std::__cxx11::stoi’
But, if I try to compile the same function by calling it in a C++ program, compilation happens successfully.
Could anyone help me figure out, what else could be done for this?
Based on the below suggestions, I updated the code in LibFunc.cpp as follows-
#include<iostream>
#include<cstdlib>
#include"data_fields.h"
#include<string>
using namespace std;
char GetCharFromHexAscii(char *asciiChar)
{
int hexVal = stoi(asciiChar, 0, 16);
return (char)hexVal;
}
On compiling this code, I got the following issues-
/tmp/ccr7aSCZ.o: In function `GetCharFromHexAscii':
LibFunc.cpp:(.text+0x24): undefined reference to `std::allocator<char>::allocator()'
LibFunc.cpp:(.text+0x3b): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
LibFunc.cpp:(.text+0x60): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
LibFunc.cpp:(.text+0x6c): undefined reference to `std::allocator<char>::~allocator()'
LibFunc.cpp:(.text+0x8f): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
LibFunc.cpp:(.text+0xa0): undefined reference to `std::allocator<char>::~allocator()'
/tmp/ccr7aSCZ.o: In function `__static_initialization_and_destruction_0(int, int)':
LibFunc.cpp:(.text+0xe0): undefined reference to `std::ios_base::Init::Init()'
LibFunc.cpp:(.text+0xf5): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccr7aSCZ.o: In function `std::__cxx11::stoi(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long*, int)':
LibFunc.cpp:(.text._ZNSt7__cxx114stoiERKNS_12basic_stringIcSt11char_traitsIcESaIcEEEPmi[_ZNSt7__cxx114stoiERKNS_12basic_stringIcSt11char_traitsIcESaIcEEEPmi]+0x1b): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const'
/tmp/ccr7aSCZ.o: In function `int __gnu_cxx::__stoa<long, int, char, int>(long (*)(char const*, char**, int), char const*, char const*, unsigned long*, int)':
LibFunc.cpp:(.text._ZN9__gnu_cxx6__stoaIlicJiEEET0_PFT_PKT1_PPS3_DpT2_EPKcS5_PmS9_[_ZN9__gnu_cxx6__stoaIlicJiEEET0_PFT_PKT1_PPS3_DpT2_EPKcS5_PmS9_]+0x62): undefined reference to `std::__throw_invalid_argument(char const*)'
LibFunc.cpp:(.text._ZN9__gnu_cxx6__stoaIlicJiEEET0_PFT_PKT1_PPS3_DpT2_EPKcS5_PmS9_[_ZN9__gnu_cxx6__stoaIlicJiEEET0_PFT_PKT1_PPS3_DpT2_EPKcS5_PmS9_]+0xa3): undefined reference to `std::__throw_out_of_range(char const*)'
/tmp/ccr7aSCZ.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
I thought this is the issue caused due to passing char * as parameter instead of passing string object to stoi function. So, I converted the char * to string object and passed it to the function. But, this didn't resolve the error and was still giving the same compilation issue.
Could anyone please help me understand why I am getting this error, and also suggest a way to resolve it?
stoiis declared in<string>, and lives in thestdnamespace.malloc(strlen(Path) + 1 * sizeof(char))only works because, by definition,sizeof (char)is1... remember multiplication has priority over addition, so your expression is equivalent tostrlen(Path) + (1 * sizeof(char))==strlen(Path) + sizeof(char)==strlen(Path) + 1... if you really want to keep the redundant multiplication by 1, domalloc((strlen(Path) + 1) * sizeof(char))... to emphasize:malloc(strlen(Path) + 1 * sizeof(char))is wrong.