#include <iostream>
#include <string.h>
char* basename(const char* filname);
int main()
{
return 0;
}
char *basename(const char* filename)
{
char* base = (char *)filename;
return base ;
}
compiling on g++ 4.1.2 20070115 (SUSE 10): No issue
compiling on g++ 4.3.4 (SUSE 11) gives following error
fileName : 9 :error:declaration of char* basename(const char*) throws different exception
fileName:3:error: from previous declaration char* basename(const char*) throw () .
Kindly tell me why this is happening , Is there is any Interface changed in g++ between these two release (if I remove inclusion of string.h then compilation success on both version of g++ ,Is any Interface change in string.h).
looks like basename already defined in string.h
# ifndef basename
/* Return the file name within directory of FILENAME. We don't
declare the function if the `basename' macro is available (defined
in <libgen.h>) which makes the XPG version of this function
available. */
# ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
extern "C++" char *basename (char *__filename)
__THROW __asm ("basename") __nonnull ((1));
extern "C++" __const char *basename (__const char *__filename)
__THROW __asm ("basename") __nonnull ((1));
# else
extern char *basename (__const char *__filename) __THROW __nonnull ((1));
# endif
# endif
It seems the name basename already exists in string.h:
http://ideone.com/Q9xSw - gives error (as it is exactly your code).
http://ideone.com/s0HIX compiles fine if you comment #include <string.h>
Related
When compiling I am getting the same warning on 3 different lines of code in a .h file such as this:
warning gnu_printf is an unrecognized format function type
My flags look like this:
CFLAGS += -Wall -Wextra -Wformat -Wno-ignored-qualifiers -Wformat-security -Wno-unused-parameter \
Examples of the three lines of code producing this error below:
int ATTR_WARN_PRINTF(1,2) OutputDebugStringF(const char* pszFormat, ...);
std::string ATTR_WARN_PRINTF(1,3) real_strprintf(const char *format, int dummy, ...);
bool ATTR_WARN_PRINTF(1,2) error(const char *format, ...);
I have many other uses of printf() in this file that are not producing any errors. I am a bit confused on the error in the formatting.
Apparently the failing code is:
#ifdef __GNUC__
#define ATTR_WARN_PRINTF(X,Y) __attribute__((format(gnu_printf,X,Y)))
#else
#define ATTR_WARN_PRINTF(X,Y)
#endif
int ATTR_WARN_PRINTF(1,2) OutputDebugStringF(const char* pszFormat, ...);
std::string ATTR_WARN_PRINTF(1,3) real_strprintf(const char *format, int dummy, ...);
bool ATTR_WARN_PRINTF(1,2) error(const char *format, ...);
It seems that this works on any gcc between versions 4.4.7 and gcc trunk (9.0.0).
GCC 4.1.2 fails with:
<source>:7: warning: 'gnu_printf' is an unrecognized format function type
Also, clang always fails on this:
<source>:7:5: warning: 'format' attribute argument not supported: gnu_printf [-Wignored-attributes]
But from the original question it seems that the issue with a GCC which is too old. To fix this, check GCC version number:
#if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__>= 4) || __GNUC__ > 4)
# define ATTR_WARN_PRINTF(X,Y) __attribute__((format(gnu_printf,X,Y)))
#elif defined(__GNUC__)
# define ATTR_WARN_PRINTF(X,Y) __attribute__((format(printf,X,Y)))
#else
# define ATTR_WARN_PRINTF(X,Y)
#endif
Maybe even it is better to restrict the format to printf instead of gnu_printf, and so the above condition can be simplified.
EDIT: As can be found on GCC history, the gnu_printf format was added in gcc-4.4.0
commit r133365. From what I understand, it is merely an alias to printf, and the gnu prefix was added to allow differentiation between printf of different compilers, such as possibly ms_printf.
I'm trying to compile a very simple program using Boost Test Unit
#define BOOST_TEST_MODULE My Test
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(first_test) { int i = 1; BOOST_CHECK(i == 1); }
If I compile this small program with no parameters,
g++ test1.cpp
there's no problem. But, if I try to use C++11 standard,
g++ test1.cpp -std=c++11
I get some errors:
In file included from /usr/include/boost/test/included/unit_test.hpp:19:0,
from test1.cpp:2: /usr/include/boost/test/impl/debug.ipp: En la función ‘const char* boost::debug::{anónimo}::prepare_gdb_cmnd_file(const boost::debug::dbg_startup_info&)’: /usr/include/boost/test/impl/debug.ipp:426:23: error: ‘::mkstemp’ no se ha declarado
fd_holder cmd_fd( ::mkstemp( cmd_file_name ) );
^ In file included from /usr/include/boost/test/included/unit_test.hpp:19:0,
from test1.cpp:2: /usr/include/boost/test/impl/debug.ipp: En la función ‘bool boost::debug::attach_debugger(bool)’: /usr/include/boost/test/impl/debug.ipp:863:34: error: ‘::mkstemp’ no se ha declarado
fd_holder init_done_lock_fd( ::mkstemp( init_done_lock_fn ) );
^ In file included from /usr/include/boost/test/utils/runtime/cla/dual_name_parameter.hpp:19:0,
from /usr/include/boost/test/impl/unit_test_parameters.ipp:31,
from /usr/include/boost/test/included/unit_test.hpp:33,
from test1.cpp:2: /usr/include/boost/test/utils/runtime/config.hpp: En la función ‘void boost::runtime::putenv_impl(boost::runtime::cstring, boost::runtime::cstring)’: /usr/include/boost/test/utils/runtime/config.hpp:95:51: error: ‘putenv’ no se declaró en este ámbito
putenv( const_cast<char*>( fs.str().c_str() ) );
(The compiler is in spanish)
I'm using:
Cygwin 64 bits
Cygwin's Boost 1.59
Cygwin's G++ 4.9.3
Any help will be welcome. Thanks.
José.-
Looks like it is a Cygwin thing. I could not reproduce it on OpenSUSE 13.2 i586 with Boost 1.54, but got the same result as yours on Cygwin Win32 with Boost 1.57. And, as Bo Persson suggested, also tried std=gnu+11.
As the compiler sayd “not declared” — even if you explicitly include <stdlib.h> which declares both mkstemp and putenv, — it seemed doubtful to me that it was all about C++ language extensions, but rather more like header file issue. Indeed, in Linux we have:
#if defined __USE_MISC || defined __USE_XOPEN_EXTENDED \
|| defined __USE_XOPEN2K8
# ifndef __USE_FILE_OFFSET64
extern int mkstemp (char *__template) __nonnull ((1)) __wur;
# else
# ifdef __REDIRECT
extern int __REDIRECT (mkstemp, (char *__template), mkstemp64)
__nonnull ((1)) __wur;
# else
# define mkstemp mkstemp64
# endif
# endif
# ifdef __USE_LARGEFILE64
extern int mkstemp64 (char *__template) __nonnull ((1)) __wur;
# endif
#endif
But in Cygwin:
#ifndef __STRICT_ANSI__
#ifndef _REENT_ONLY
int _EXFUN(mkstemp,(char *));
#endif
int _EXFUN(_mkstemp_r, (struct _reent *, char *));
#endif
Then I added a couple of #undefs to your program:
#undef __STRICT_ANSI__
#undef _REENT_ONLY
#define BOOST_TEST_MODULE My Test
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(first_test) { int i = 1; BOOST_CHECK(i == 1); }
And could compile it fine with std=c++11. I have no idea how incorrect and stupid this may be, but at least it produced very similar exe file that only differs by 20 bytes (aside from fingerprint).
I have a file "test.cxx" with
namespace net {
extern "C" {
#include <arpa/inet.h>
}
}
int main() {
htons(1024);
}
When compiling with -O1 or more everything's fine.
When compiling with -O0:
error: ‘htons’ was not declared in this scope
suggested alternative: ‘net::htons’
Then I change htons to net::htons.
When compiling with -O0 everything's fine.
When compiling with -O1 or more:
error: expected unqualified-id before ‘(’ token
Reproduced that on gcc-4.9.2 and clang-3.7.0.
Can someone explain why does it happen?
It happens because at -O0, call is compiled to htons function and your declaration for this function is inside namespace net. In optimized version, -O2 for example, call is replaced with a macro.
You can verify this by pre-compiling your program using gcc -O0 -E v/s gcc -O2 -E
When htons is used
At -O2, htons is translated to
int main() {
(__extension__ (
{
register unsigned short int __v, __x = (unsigned short int) (1024);
if (__builtin_constant_p (__x))
__v = ((unsigned short int) ((((__x) >> 8) & 0xff) | (((__x) & 0xff) << 8)));
else
__asm__ ("rorw $8, %w0" : "=r" (__v) : "0" (__x) : "cc");
__v;
}
));
}
Making the code not throw the resolution error.
error: ‘htons’ was not declared in this scope
When net::htons is used
When you replace ntohs with net::ntohs, ntohs define is used / exposed for optimization and your pre-processed code look as:
int main() {
net::(__extension__ ({... /* removed for Brevity */ ...}));
}
And hence the error
error: expected unqualified-id before ‘(’ token
Why does it happenhtons may be implemented as a function or a macro. If it is defined as macro, htons would work fine. But if it is defined as a function net::htons would work fine.
It appears at -O1 or higher, header files expose macro versions instead of function.
Possible solutions
using namespace net; // Not recommended
#ifndef htons // Recommended
using net::htnos;
#endif
extern "C" { // Add all declarations in global space
#include <arpa/inet.h>
}
I have tried a lot, but could not figure out what the errors are.
Any detailed explanation about the this will be very helpful.
So basically i am trying to write a re-entrant parser and these are my files.
Lex1.ll
%{
#include "Globals.h"
#include "yac1.tab.hh"
extern "C"
{
int yylex(void);
}
%}
alpha [A-Za-z]
digit [0-9]
%option case-insensitive
%option bison-bridge
%option reentrant
%option noyywrap
%%
"DELETE ALL" return DELALL;
"INSERT" return INSERT;
DELETE return DELETE;
FIND return FIND;
(\+|\-)?[0-9]+ { return INT; }
\n { return ENDL; }
. ;
%%
yac1.yy
%{
#include <stdio.h>
#include "Globals.h"
%}
%pure-parser
%error-verbose
%{
#define YYPARSE_PARAM parm
#define YYLEX_PARAM ((struct parsed_vals *)parm)->scanner
#define cast ((struct parsed_vals *) parm)
void yyerror(struct parsed_vals * parm,const char *s)
{
fprintf(stderr, "error: %s\n", s);
}
extern "C"
{
int yylex(void );
// int yywrap()
// {
// return 1;
// }
}
%}
%token INSERT DELETE DELALL FIND ENDL
%union {
int ival;
float fval;
char *sval;
}
%token <ival> INT
%token <fval> FLOAT
%token <sval> STRING
%%
S:T
T: INSERT val {cast->cmd=INSERT_CMD;}
| DELETE val {cast->cmd=DELETE_CMD;}
;
val : INT ENDL {cast->type=INT_TYPE;
(cast->data).int_data=$1;}
|
FLOAT ENDL {cast->type=FLOAT_TYPE;
(cast->data).float_data=$1;}
|
STRING ENDL {cast->type=STRING_TYPE;
(cast->data).str_data=$1;}
;
%%
My main function testlex.cc
#include <stdio.h>
#include "Globals.h"
#include "lexheader.h"
#include "yac1.tab.hh"
int yyparse(void *);
yyscan_t scanner;
main()
{
struct parsed_vals foo;
const char * buffer = "inseRt 4\n";
yylex_init(&(foo.scanner));
yyset_extra(&foo, foo.scanner);
YY_BUFFER_STATE bp = yy_scan_string( buffer, foo.scanner );
yy_switch_to_buffer(bp, foo.scanner);
int a;
int ret_val = yyparse(&foo);
yy_delete_buffer(bp, foo.scanner);
if(ret_val!=0) printf("False");
printf ("hello %d\n",foo.data.int_data);
printf ("hello %d\n",foo.type);
yylex_destroy(foo.scanner);
}
Globals.h
/*
* File: Globals.h
* Author: atghosh
*
* Created on 3 August, 2013, 8:39 PM
*/
#ifndef GLOBALS_H
#define GLOBALS_H
enum CMD {INSERT_CMD=1, DELETE_CMD, FIND_CMD, DELALL_CMD};
enum TYPE {INT_TYPE=5, FLOAT_TYPE, STRING_TYPE};
struct parsed_vals{
int cmd;
int type;
union{
int int_data;
float float_data;
char *str_data;
} data;
void * scanner;
};
#endif /* GLOBALS_H */
Makefile
parser: lex1.ll yac1.yy testlex.cc
bison -d yac1.yy
flex --header-file="lexheader.h" lex1.ll
g++ -o parser yac1.tab.cc lex.yy.c testlex.cc -lfl
clean:
rm -rf *.o parser lexheader.h lex.yy.c lex.yy.cc parser yac1.tab.cc yac1.tab.hh
And my error list
bison -d yac1.yy
flex --header-file="lexheader.h" lex1.ll
g++ -o parser yac1.tab.cc lex.yy.c testlex.cc -lfl
yac1.tab.cc: In function ‘int yyparse(void*)’:
yac1.tab.cc:1302:16: error: too many arguments to function ‘int yylex()’
yac1.yy:20:13: note: declared here
yac1.tab.cc:1510:24: error: cannot convert ‘const char*’ to ‘parsed_vals*’ for argument ‘1’ to ‘void yyerror(parsed_vals*, const char*)’
yac1.tab.cc:1625:35: error: cannot convert ‘const char*’ to ‘parsed_vals*’ for argument ‘1’ to ‘void yyerror(parsed_vals*, const char*)’
In file included from testlex.cc:3:0:
lexheader.h:278:1: error: ‘YYSTYPE’ does not name a type
lexheader.h:280:18: error: variable or field ‘yyset_lval’ declared void
lexheader.h:280:18: error: ‘YYSTYPE’ was not declared in this scope
lexheader.h:280:28: error: ‘yylval_param’ was not declared in this scope
lexheader.h:280:51: error: expected primary-expression before ‘yyscanner’
lexheader.h:328:17: warning: ‘yylex’ initialized and declared ‘extern’ [enabled by default]
lexheader.h:328:17: error: ‘YYSTYPE’ was not declared in this scope
lexheader.h:328:27: error: ‘yylval_param’ was not declared in this scope
lexheader.h:328:50: error: expected primary-expression before ‘yyscanner’
lexheader.h:328:59: error: expression list treated as compound expression in initializer [-fpermissive]
make: *** [parser] Error 1
I am just not able to figure out what is going wrong.
Expecting detailed reply and no links.
I have already referred to these links
http://www.phpcompiler.org/articles/reentrantparser.html
http://plindenbaum.blogspot.in/2009/12/parsing-genetic-code-using-flex-and_14.html
Here's a number of issues and the corresponding fixes.
Erase the prototype of yylex from both lex1.ll and yac1.yy. You shouldn't have to define it yourself anywhere.
Add two #include's near the beginning of yac1.yy:
#include "yac1.tab.hh"
#include "lexheader.h"
Make sure you have them in this order, as the first one defines YYSTYPE which is used by the second one. This is a known issue with flex/bison lexers and parsers.
Fix the prototype and the definition of yyerror in yac1.yy. It should be:
void yyerror(const char *s);
If you need an extra parameter there, for your own purpose from your own calls, you
cannot expect the parser to provide it. In this case, define your own error handler
and use a different name.
After all this, your program compiles using your Makefile. Whether it works as expected or not, I cannot tell.
I met some compilation error but do not know what the problem is. The code seems not use exception, but the error is about it.
//in misc.h:
char *basename(char *name); // line 94
// in misc.cc:
char *basename(char *name) { // line 12
char *result = name;
while(*name) {
if(*name == '/') result = name + 1;
name++;
}
return result;
}
Compilation error
g++ -pipe -W -Wall -fopenmp -ggdb3 -O2 -c -o misc.o ../../src/misc.cc
../../src/misc.cc: In function ‘char* basename(char*)’:
../../src/misc.cc:12: error: declaration of ‘char* basename(char*)’ throws different exceptions
../../src/misc.h:94: error: from previous declaration ‘char* basename(char*) throw ()’
make: *** [misc.o] Error 1
Does someone have some clue? Thanks and regards!
EDIT:
Files included in misc.h are
#include <iostream>
#include <cmath>
#include <fstream>
#include <cfloat>
#include <stdlib.h>
#include <string.h>
EDIT:
in misc.i generated by -E option,
extern "C++" char *basename (char *__filename)
throw () __asm ("basename") __attribute__ ((__nonnull__ (1)));
extern "C++" __const char *basename (__const char *__filename)
throw () __asm ("basename") __attribute__ ((__nonnull__ (1)));
# 640 "/usr/include/string.h" 3 4
# 1 "/usr/include/bits/string3.h" 1 3 4
# 23 "/usr/include/bits/string3.h" 3 4
extern void __warn_memset_zero_len (void) __attribute__((__warning__ ("memset used with constant zero length parameter; this could be due to transposed parameters")));
# 48 "/usr/include/bits/string3.h" 3 4
extern __inline __attribute__ ((__always_inline__)) __attribute__ ((__gnu_inline__, __artificial__)) void *
memcpy (void *__restrict __dest, __const void *__restrict __src, size_t __len) throw ()
{
return __builtin___memcpy_chk (__dest, __src, __len, __builtin_object_size (__dest, 0));
}
...
# 641 "/usr/include/string.h" 2 3 4
...
You may be picking up the definition of basename() from libgen.h. On my OpenSUSE system, the version in libgen.h is defined with "throw ()" at the end (via the __THROW macro).
One thing you can try is to tell gcc to only run the preprocessor stage by adding the -E flag and then search for basename to see what is being defined:
g++ -pipe -W -Wall -fopenmp -ggdb3 -O2 -E -o misc.i ../../src/misc.cc
If that is happening, you'll either need to drop the include of libgen.h, match the throw specifier or change the name of your function.
" ../../src/misc.h:94: error: from previous declaration ‘char* basename(char*) throw ()’ "
I'm reading that as having been declared twice, once with throw() and once without.
Compiles for me, same flags.
g++ (Gentoo 4.3.4 p1.0, pie-10.1.5) 4.3.4
Your error says that there is a declaration of ‘char* basename(char*) throw ()’
try opening misc.h and searching for throw in the entire file, to see if you put the throw in yourself and just forgot about it.