1

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?

4
  • 2
    You need to compile the C and C++ separately and then link them together. Commented Aug 18, 2020 at 15:15
  • 1
    stoi is declared in <string>, and lives in the std namespace. Commented Aug 18, 2020 at 15:18
  • malloc(strlen(Path) + 1 * sizeof(char)) only works because, by definition, sizeof (char) is 1 ... remember multiplication has priority over addition, so your expression is equivalent to strlen(Path) + (1 * sizeof(char)) == strlen(Path) + sizeof(char) == strlen(Path) + 1 ... if you really want to keep the redundant multiplication by 1, do malloc((strlen(Path) + 1) * sizeof(char)) ... to emphasize: malloc(strlen(Path) + 1 * sizeof(char)) is wrong. Commented Aug 19, 2020 at 14:41
  • 1
    @pmg: Thanks for correcting me. Commented Aug 19, 2020 at 14:48

2 Answers 2

1

You call a function stoi. You haven't defined such function.

Like the error message also noticed, you may have instead intended to call std::stoi function from the C++ standard library. Notice that it is declared in the std namespace. Furthermore, you should include the header which declares it (<string>).


In addition, you may need to link with the C++ standard library. This would happen automatically if you used g++ frontend instead of gcc.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank u for the suggestion of using std, as that helped me move a step further. But, compiling with g++ doesn't help me resolve my issues, it rather gives me more compilation issues, since my other files are .c files and are properly compiled using gcc only.
@Priya.D Compiler C++ files with g++ and C files with gcc.
1

The function stoi is located in the <string> header (not string.h, you can check https://en.cppreference.com/w/cpp/string/basic_string/stol).

Since there is no using namespace std in your code, you should change your call to stoi to std::stoi

A final working code can look something like this

#include <string>
char GetCharFromHexAscii(char *asciiChar)
{
    int hexVal = std::stoi(asciiChar, 0, 16);
     return (char)hexVal;
}

1 Comment

Thank u for the suggestion of using std, as this helped me move a step further, but, I got another set of errors. I've updated the above question with the next set of errors. Kindly check and let me know if u can help me with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.