Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: 'strdup' was not declared in this scope

I am trying to Build and Install the Apache Thrift compiler and libraries

As shown in instructions run ./configure && make

And I get this error:

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

I edited thrifty.yy and added #include <string.h> yet I still get the same error that strdup is missing.

src/thrifty.yy:1311:30: error: 'strdup' was not declared in this scope (same error as before including string.h)

What am I missing here ?

Thanks in advance!

like image 780
Tony Tannous Avatar asked Nov 23 '16 13:11

Tony Tannous


People also ask

Can strdup fail?

The strdup () and strndup () functions may fail and set the external variable errno for any of the errors specified for the library function malloc(3).

Do I need to malloc before strdup?

You don't need to do a malloc() yourself; indeed, it immediately leaks if you do it since you overwrite the only pointer to the space you allocated with the pointer to the space that strdup() allocated. Hence: char *variable = strdup(word); if (variable == 0) …

What does the function strdup do?

The strdup() and strndup() functions are used to duplicate a string. strdup() : Syntax : char *strdup(const char *s); This function returns a pointer to a null-terminated byte string, which is a duplicate of the string pointed to by s.

Is strdup a standard?

Most C programmers are familiar with the strdup function. Many of them will take it for granted, yet it is not part of the C Standard (neither C89, C99 nor C11). It is part of POSIX and may not be available on all environments.


1 Answers

strdup is not a standard C function. When a compiler is configured to be strict C compliant, it is not allowed to dump its own custom, non-standard functions in standard library headers like <string.h>.

You can resolve this by changing the compiler to compile non-standard C code (for example in gcc, compile with -std=gnu11 instead of -std=c11). Or alternatively, stick to pure standard C.


... or just implement strdup yourself, it is easy:

#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;
}
like image 182
Lundin Avatar answered Sep 21 '22 17:09

Lundin