8

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!

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
  • http://stackoverflow.com/questions/32944390/what-is-the-rationale-for-not-including-strdup-in-the-c-standard – Steephen Nov 23 '16 at 13:42
  • Anything related in config.log? – JensG Nov 23 '16 at 14:20
  • Is that cygwin environment? Woudl be nice to know ... in that case look at [THRIFT-2800](https://issues.apache.org/jira/browse/THRIFT-2800) – JensG Nov 23 '16 at 14:24
  • ... or use the [existing Windows binary](http://stackoverflow.com/a/40767099/499466). – JensG Nov 23 '16 at 14:29

3 Answers3

15

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;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    @TonyTannous Don't compile C code with a C++ compiler. That being said, there's no reason why a C++ compiler wouldn't recognize pointers, more likely it doesn't recognize `` but gives bad errors. – Lundin Nov 23 '16 at 14:07
  • It is not me, all I did was ./configure && make as in the instructions. https://thrift.apache.org/tutorial/ – Tony Tannous Nov 23 '16 at 14:09
  • I reopened the file and the only place I have a strdup is `{ pdebug("TypeAnnotationValue ->"); $$ = strdup("1"); }` I will try to implement it otherway. Thanks though :) – Tony Tannous Nov 23 '16 at 14:17
  • 2
    Maybe it solves the actual problem, but it should not be necessary to fiddle with source or make files at all. That's very likely only a workaround, not the real solution. – JensG Nov 23 '16 at 14:21
  • 3
    I had to add a cast: `char* result = (char*) malloc(slen + 1);` – robermann Aug 23 '18 at 13:49
  • @robermann That's because you are using the wrong compiler. You shouldn't compile C code on a C++ compiler. – Lundin Aug 23 '18 at 13:51
  • Actually I was trying to compile c++ code ([this leveldb](https://github.com/bitcoin-core/leveldb/blob/bitcoin-fork/db/c.cc) on Windows + Cygwin); then I got the same OP's message. After your suggestion, I got: In function ‘char* strdup(const char*)’: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive] char* result = malloc(slen + 1); With the cast worked, but I got other problems (ok, C/C++ is not my main strength :)) – robermann Aug 23 '18 at 19:01
  • 2
    @robermann C and C++ are two different languages, incompatible in numerous ways. – Lundin Aug 24 '18 at 06:29
3

In my case, in MakeFile by directory /thrift-x.x.x/compiler/cpp/src replace -std=c++11 to -std=gnu++11

donjuedo
  • 2,475
  • 18
  • 28
2

./configure CXXFLAGS='-std=gnu++11' && make

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 12 '22 at 19:30