[英]Error compiling a C program which calls a C++ function declared in header file and defined in a separate cpp file
我正在嘗試在 C 程序中調用 C++ function。 我在 ah 文件中聲明 function 如下 - data_fields.h:
#ifdef __cplusplus
extern "C"
{
#endif
char GetCharFromHexAscii(char *);
#ifdef __cplusplus
}
#endif
我在 my.c 文件中調用 GetCharFromHexAscii() 如下 - 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;
}
C++ function在.cpp中的定義如下-LibFunc.cpp:
#include<iostream>
#include<cstdlib>
#include"data_fields.h"
char GetCharFromHexAscii(char *asciiChar)
{
int hexVal = stoi(asciiChar, 0, 16);
return (char)hexVal;
}
這是我用來編譯我的 C 代碼的命令-
gcc GUIForRenamer.c -lm `pkg-config --libs --cflags gtk+-3.0` -export-dynamic FileExtensionRenamer.c LibFunc.cpp
但是編譯失敗並出現以下錯誤-
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’
但是,如果我嘗試通過在 C++ 程序中調用它來編譯相同的 function,編譯就會成功。
誰能幫我弄清楚,還可以為此做些什么?
根據以下建議,我將 LibFunc.cpp 中的代碼更新如下 -
#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;
}
在編譯這段代碼時,我遇到了以下問題——
/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
我認為這是由於將 char * 作為參數傳遞而不是將字符串 object 傳遞給 stoi function 而導致的問題。因此,我將 char * 轉換為字符串 object 並將其傳遞給 function。但是,這並沒有解決錯誤並且仍然給出相同的編譯問題。
誰能幫我理解為什么會出現此錯誤,並提出解決方法?
你打電話給 function stoi
。 您還沒有定義這樣的 function。
就像還注意到的錯誤消息一樣,您可能打算從 C++ 標准庫調用std::stoi
function。 請注意,它是在std
命名空間中聲明的。 此外,您應該包括聲明它的 header ( <string>
)。
此外,您可能需要鏈接 C++ 標准庫。 如果您使用g++
前端而不是gcc
,這將自動發生。
function stoi
位於<string>
header(不是 string.h,您可以查看https://en.cppreference.com/w/cpp/string/basic_string/stol )。
由於您的代碼中沒有using namespace std
,因此您應該stoi
的調用更改為std::stoi
最終的工作代碼看起來像這樣
#include <string>
char GetCharFromHexAscii(char *asciiChar)
{
int hexVal = std::stoi(asciiChar, 0, 16);
return (char)hexVal;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.