error: 'strdup' was not declared in this scope
error: 'strdup' was not declared in this scope
我正在尝试构建和安装 Apache Thrift 编译器和库
如图说明运行./configure && make
我得到这个错误:
thrift 0.9.3
Building C++ Library ......... : no
Building C (GLib) Library .... : no
Building Java Library ........ : no
Building C# Library .......... : no
Building Python Library ...... : no
Building Ruby Library ........ : no
Building Haxe Library ........ : no
Building Haskell Library ..... : no
Building Perl Library ........ : no
Building PHP Library ......... : no
Building Erlang Library ...... : no
Building Go Library .......... : no
Building D Library ........... : no
Building NodeJS Library ...... : no
Building Lua Library ......... : no
If something is missing that you think should be present,
please skim the output of configure to find the missing
component. Details are present in config.log.
make all-recursive
make[1]: Entering directory '/c/University/InternetOfThings/thrift-0.9.3'
Making all in compiler/cpp
make[2]: Entering directory '/c/University/InternetOfThings/thrift-0.9.3/compiler/cpp'
/bin/sh ../../ylwrap src/thrifty.yy y.tab.c src/thrifty.cc y.tab.h `echo src/thrifty.cc | sed -e s/cc$/hh/ -e s/cpp$/hpp/ -e s/cxx$/hxx/ -e s/c++$/h++/ -e s/c$/h/` y.output src/thrifty.output -- bison -y -d
updating src/thrifty.hh
make all-am
make[3]: Entering directory '/c/University/InternetOfThings/thrift-0.9.3/compiler/cpp'
g++ -DHAVE_CONFIG_H -I. -I../.. -I../../lib/cpp/src/thrift -I./src -Wall -Wno-sign-compare -Wno-unused -g -O2 -std=c++11 -MT src/libparse_a-thrifty.o -MD -MP -MF src/.deps/libparse_a-thrifty.Tpo -c -o src/libparse_a-thrifty.o `test -f 'src/thrifty.cc' || echo './'`src/thrifty.cc
src/thrifty.yy: In function 'int yyparse()':
src/thrifty.yy:1311:30: error: 'strdup' was not declared in this scope
Makefile:912: recipe for target 'src/libparse_a-thrifty.o' failed
make[3]: *** [src/libparse_a-thrifty.o] Error 1
make[3]: Leaving directory '/c/University/InternetOfThings/thrift-0.9.3/compiler/cpp'
Makefile:588: recipe for target 'all' failed
make[2]: *** [all] Error 2
make[2]: Leaving directory '/c/University/InternetOfThings/thrift-0.9.3/compiler/cpp'
Makefile:609: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/c/University/InternetOfThings/thrift-0.9.3'
Makefile:530: recipe for target 'all' failed
make: *** [all] Error 2
我编辑了 thrifty.yy 并添加了 #include <string.h>
但我仍然得到同样的错误,即缺少 strdup。
src/thrifty.yy:1311:30: error: 'strdup' was not declared in this scope
(与之前相同的错误,包括 string.h)
我在这里错过了什么?
提前致谢!
strdup
不是标准的 C 函数。当编译器配置为严格符合 C 时,不允许将其自定义的非标准函数转储到标准库头文件中,例如 <string.h>
.
您可以通过将编译器更改为编译非标准 C 代码来解决此问题(例如,在 gcc 中,使用 -std=gnu11
而不是 -std=c11
进行编译)。或者,坚持使用纯标准 C。
...或者自己实现strdup,很简单:
#include <string.h>
#include <stdlib.h>
char* strdup (const char* s)
{
size_t slen = strlen(s);
char* result = malloc(slen + 1);
if(result == NULL)
{
return NULL;
}
memcpy(result, s, slen+1);
return result;
}
在我的例子中,在目录 /thrift-x.x.x/compiler/cpp/src
的 MakeFile 中将 -std=c++11
替换为 -std=gnu++11
./configure CXXFLAGS='-std=gnu++11' && make
我正在尝试构建和安装 Apache Thrift 编译器和库
如图说明运行./configure && make
我得到这个错误:
thrift 0.9.3
Building C++ Library ......... : no
Building C (GLib) Library .... : no
Building Java Library ........ : no
Building C# Library .......... : no
Building Python Library ...... : no
Building Ruby Library ........ : no
Building Haxe Library ........ : no
Building Haskell Library ..... : no
Building Perl Library ........ : no
Building PHP Library ......... : no
Building Erlang Library ...... : no
Building Go Library .......... : no
Building D Library ........... : no
Building NodeJS Library ...... : no
Building Lua Library ......... : no
If something is missing that you think should be present,
please skim the output of configure to find the missing
component. Details are present in config.log.
make all-recursive
make[1]: Entering directory '/c/University/InternetOfThings/thrift-0.9.3'
Making all in compiler/cpp
make[2]: Entering directory '/c/University/InternetOfThings/thrift-0.9.3/compiler/cpp'
/bin/sh ../../ylwrap src/thrifty.yy y.tab.c src/thrifty.cc y.tab.h `echo src/thrifty.cc | sed -e s/cc$/hh/ -e s/cpp$/hpp/ -e s/cxx$/hxx/ -e s/c++$/h++/ -e s/c$/h/` y.output src/thrifty.output -- bison -y -d
updating src/thrifty.hh
make all-am
make[3]: Entering directory '/c/University/InternetOfThings/thrift-0.9.3/compiler/cpp'
g++ -DHAVE_CONFIG_H -I. -I../.. -I../../lib/cpp/src/thrift -I./src -Wall -Wno-sign-compare -Wno-unused -g -O2 -std=c++11 -MT src/libparse_a-thrifty.o -MD -MP -MF src/.deps/libparse_a-thrifty.Tpo -c -o src/libparse_a-thrifty.o `test -f 'src/thrifty.cc' || echo './'`src/thrifty.cc
src/thrifty.yy: In function 'int yyparse()':
src/thrifty.yy:1311:30: error: 'strdup' was not declared in this scope
Makefile:912: recipe for target 'src/libparse_a-thrifty.o' failed
make[3]: *** [src/libparse_a-thrifty.o] Error 1
make[3]: Leaving directory '/c/University/InternetOfThings/thrift-0.9.3/compiler/cpp'
Makefile:588: recipe for target 'all' failed
make[2]: *** [all] Error 2
make[2]: Leaving directory '/c/University/InternetOfThings/thrift-0.9.3/compiler/cpp'
Makefile:609: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/c/University/InternetOfThings/thrift-0.9.3'
Makefile:530: recipe for target 'all' failed
make: *** [all] Error 2
我编辑了 thrifty.yy 并添加了 #include <string.h>
但我仍然得到同样的错误,即缺少 strdup。
src/thrifty.yy:1311:30: error: 'strdup' was not declared in this scope
(与之前相同的错误,包括 string.h)
我在这里错过了什么?
提前致谢!
strdup
不是标准的 C 函数。当编译器配置为严格符合 C 时,不允许将其自定义的非标准函数转储到标准库头文件中,例如 <string.h>
.
您可以通过将编译器更改为编译非标准 C 代码来解决此问题(例如,在 gcc 中,使用 -std=gnu11
而不是 -std=c11
进行编译)。或者,坚持使用纯标准 C。
...或者自己实现strdup,很简单:
#include <string.h>
#include <stdlib.h>
char* strdup (const char* s)
{
size_t slen = strlen(s);
char* result = malloc(slen + 1);
if(result == NULL)
{
return NULL;
}
memcpy(result, s, slen+1);
return result;
}
在我的例子中,在目录 /thrift-x.x.x/compiler/cpp/src
的 MakeFile 中将 -std=c++11
替换为 -std=gnu++11
./configure CXXFLAGS='-std=gnu++11' && make