I have the following code...
// My.h
#include <string>
....
// My.cpp
#include "My.h"
...
errMsg = "X value [too low]: " + std::to_string(xInVal);
But when I compile like...
//setup.py
extra_compile_args =['-std=c++11']
//console output
gcc -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -ggdb -O2 -pipe -Wimplicit-function-declaration -fdebug-prefix-map=/usr/src/ports/python3/python3-3.4.3-1.x86_64/build=/usr/src/debug/python3-3.4.3-1 -fdebug-prefix-map=/usr/src/ports/python3/python3-3.4.3-1.x86_64/src/Python-3.4.3=/usr/src/debug/python3-3.4.3-1 -I./My -c ./My.cpp -o build/temp.cygwin-2.2.1-x86_64-3.4/./My.o -std=c++11
I get the following error....
error: ‘to_string’ is not a member of ‘std’
errMsg = "X value [too low]: " + std::to_string(xInVal);
What am I missing how do I use to_string in this way?
From the CYGWIN headers, it looks like for C++11 these are not defined unless _GLIBCXX_USE_C99 is defined They are enclosed in this if:
#if __cplusplus >= 201103L && defined(_GLIBCXX_USE_C99)
I tried defining _GLIBCXX_USE_C99 but that didn't work for my project. You can try defining it before you use the string header.
I recently had a similar issue with C++11 on CYGWIN related to __STRICT_ANSI__
Related
For this struct and function:
typedef struct data_s
{
int i1;
int i2;
} data_t;
void print_data_passed_by_ptr(const data_t *data)
{
printf(" i1 = %i\n"
" i2 = %i\n\n",
data->i1,
data->i2);
}
this works fine in C:
// Print R-value struct passed by ptr
print_data_passed_by_ptr(&(data_t){
.i1 = 7,
.i2 = 8,
});
My C build command is:
gcc -Wall -Wextra -Werror -O3 -std=c17 \
struct_pass_R_values_by_cpp_reference_and_ptr.c -o bin/a && bin/a
But, in C++ it fails with
error: taking address of temporary [-fpermissive]
My C++ build command is:
g++ -Wall -Wextra -Werror -O3 -std=c++17 \
struct_pass_R_values_by_cpp_reference_and_ptr.c -o bin/a && bin/a
So, I added -fpermissive to my C++ build command:
g++ -Wall -Wextra -Werror -O3 -std=c++17 -fpermissive \
struct_pass_R_values_by_cpp_reference_and_ptr.c -o bin/a && bin/a
and now the C++ build fails with this:
error: taking address of temporary [-Werror=permissive]
I tried turning off -Werror=permissive with -Wno-error=permissive (see here for that GCC documentation: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html, and search the page for -Wno-error=), but that's not a valid option. New build cmd I attempted:
g++ -Wall -Wextra -Werror -O3 -std=c++17 -fpermissive -Wno-error=permissive \
struct_pass_R_values_by_cpp_reference_and_ptr.c -o bin/a && bin/a
...fails with:
cc1plus: error: -Werror=permissive: no option -Wpermissive
So, how do I solve this to force this C code to build in C++? Either suppressing the warning/error, OR providing some modification to the code other than the one shown just below are acceptable answers. I want the code to compile as C also, not just C++, in the end.
I know I can use "const reference" in C++ instead of ptr, like this, and that's great and all and it might answer somebody else's question, but that's not my question:
void print_data_passed_by_cpp_reference(const data_t& data)
{
printf(" i1 = %i\n"
" i2 = %i\n\n",
data.i1,
data.i2);
}
// Print R-value struct passed by C++ reference
print_data_passed_by_cpp_reference({
.i1 = 9,
.i2 = 10,
});
I also know I can remove -Werror and keep -fpermissive to make it build, with warnings, like this:
eRCaGuy_hello_world/cpp$ g++ -Wall -Wextra -O3 -std=c++17 -fpermissive \
struct_pass_R_values_by_cpp_reference_and_ptr.c -o bin/a && bin/a
struct_pass_R_values_by_cpp_reference_and_ptr.c: In function ‘int main()’:
struct_pass_R_values_by_cpp_reference_and_ptr.c:87:5: warning: taking address of temporary [-fpermissive]
});
^
Hello world.
i1 = 7
i2 = 8
i1 = 9
i2 = 10
...but I'd really like to keep -Werror on and make that warning go away.
How to automatically pass an R-value parameter into a function as a const ptr for C and as a const reference for C++
(emphasis added to my original quote)
So, how do I solve this to force this C code to build in C++? Either suppressing the warning/error, OR providing some modification to the code other than the one shown just below are acceptable answers. I want the code to compile as C also, not just C++, in the end.
This works! It is one approach. If there are ways to disable the warning/error in gcc via command-line options I'd still like to know those though.
This is pretty clever I think. It passes the R-value by const ptr for C and by const reference for C++ by using two separate definitions for the print_data() function and the DATA_T macro, depending on the language.
#ifndef __cplusplus
// For C
void print_data(const data_t *data)
{
printf(" i1 = %i\n"
" i2 = %i\n\n",
data->i1,
data->i2);
}
#else
// For C++
void print_data(const data_t& data)
{
printf(" i1 = %i\n"
" i2 = %i\n\n",
data.i1,
data.i2);
}
#endif
#ifndef __cplusplus
// For C
#define DATA_T &(data_t)
#else
// For C++
#define DATA_T // leave empty
#endif
Usage:
// Print R-value struct passed by C++ reference, OR by C ptr, depending on
// whether this code is compiled as C or C++
print_data(DATA_T{
.i1 = 9,
.i2 = 10,
});
Build commands:
# For C
gcc -Wall -Wextra -Werror -O3 -std=c17 \
struct_pass_R_values_by_cpp_reference_and_ptr.c -o bin/a && bin/a
# For C++
g++ -Wall -Wextra -Werror -O3 -std=c++17 \
struct_pass_R_values_by_cpp_reference_and_ptr.c -o bin/a && bin/a
I got the following warning when compiling a C++ file :
variables.cpp:10:8: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int c{2} ;
This is the file :
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std ;
int main()
{
int a = 0 ;
int b(1) ;
int c{2} ;
string myString = "I am a string !" ;
cout << a+b+c << endl ;
cout << myString << endl ;
return EXIT_SUCCESS ;
}
And this is the command line :
g++ -std=c++0x -Wall -Wextra -Winit-self -Wold-style-cast -Woverloaded-virtual -Wuninitialized -Wmissing-declarations -Winit-self -ansi -pedantic variables.cpp -o variables
I am using g++ 7.4.0 on Ubuntu 18.04.1
I do not want to ignore the warning but to solve it,
Thank you
PS : I tried to change -std=c++0x to -std=c++11 but it did not change anything
Remove -ansi in your command, which is equivalent to -std=c++98 and would overwrite your previous flag -std=c++11. According to C-Dialect-Options,
In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98.
Replace -std=c++0x with -std=c++11.
Note that if your compiler supports it, it is recommended to use the lastest c++ standard which is -std=c++17. Using newer c++ standard usually makes your code shorter, more readable and more performant.
You have 2 problem in compilation command line:
The first one is the -ansi in compilation command that implicitly set the standard to the c++98. In you case the -ansi option generate conflict with -std=c++11.
The second one is the -std=c++0x, you have to replace it with -std=c++11.
My environment Arch Linux, gcc 7.2
I'm learning C++ and I'm using keyword constexpr to define a constant, while compile, it give me an error message
error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
I can compile my program with default g++, but cannot compile with -std=c++14 and -Werror
The command I'm using is:
g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
I believe the -Werror option caused the issue. but what is the issue? can someone tell me please?
#include <iostream>
int main() {
constexpr double yen_dollar = 0.107;
std::cout << yen_dollar << std::endl;
return 0;
}
test.cpp:4:5: error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
constexpr double yen_dollar = 0.107;
^~~~~~~~~
test.cpp: In function ‘int main()’:
test.cpp:4:5: error: ‘constexpr’ was not declared in this scope
test.cpp:5:16: error: ‘yen_dollar’ was not declared in this scope
std::cout << yen_dollar << std::endl;
From the GCC documentation §3.4 Options Controlling C Dialect, one can read:
-ansi
In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98.
Since, you compiled with
g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
-ansi overwrites -std=c++14 with -std=c++98. This is why constexpr is not recognized.
Solution: get rid of the -ansi flag.
I don't understand why RubyInline is crashing.
class CPPCode
inline do |builder|
builder.include '<algorithm>'
builder.include '<vector>'
builder.c 'int test(){return 1;}'
end
end
The error:
error executing "gcc -shared -fPIC -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -fPIC -L. -fstack-protector -rdynamic -Wl,-export-dynamic -I /home/lionzxy/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0 -I /home/lionzxy/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0/x86_64-linux -I /home/lionzxy/.rvm/rubies/ruby-2.3.0/include -L/home/lionzxy/.rvm/rubies/ruby-2.3.0/lib -o \"/home/lionzxy/.ruby_inline/ruby-2.3.0/Inline_Book__CPPCode_232b56c4fe2ef7959c8f3c1f6db3cebb.so\" \"/home/lionzxy/.ruby_inline/ruby-2.3.0/Inline_Book__CPPCode_232b56c4fe2ef7959c8f3c1f6db3cebb.c\" ": pid 24454 exit 1 Renamed /home/lionzxy/.ruby_inline/ruby-2.3.0/Inline_Book__CPPCode_232b56c4fe2ef7959c8f3c1f6db3cebb.c to /home/lionzxy/.ruby_inline/ruby-2.3.0/Inline_Book__CPPCode_232b56c4fe2ef7959c8f3c1f6db3cebb.c.bad
Generated C++ file:
#include "ruby.h"
#include <algorithm>
#include <vector>
# line 58 "/home/lionzxy/RubymineProjects/app/models/book.rb"
static VALUE test(VALUE self) {
return INT2FIX(1);}
#ifdef __cplusplus
extern "C" {
#endif
void Init_Inline_Book__CPPCode_098f6bcd4621d373cade4e832627b4f6() {
VALUE c = rb_cObject;
c = rb_const_get(c, rb_intern("Book"));
c = rb_const_get(c, rb_intern("CPPCode"));
rb_define_method(c, "test", (VALUE(*)(ANYARGS))test, 0);
}
#ifdef __cplusplus
}
#endif
You're trying to use C++ code, but you're running gcc without any flags to tell it that it's C++ code.
On the first documentation page we see an example on how to use C++ code:
require 'inline'
class MyTest
inline(:C) do |builder|
builder.include '<iostream>'
builder.add_compile_flags '-x c++', '-lstdc++'
builder.c '
void hello(int i) {
while (i-- > 0) {
std::cout << "hello" << std::endl;
}
}'
end
end
t = MyTest.new()
t.hello(3)
The "trick" here is:
builder.add_compile_flags '-x c++', '-lstdc++'
which tells gcc that this is C++ code.
This question already has answers here:
C++ mutex in namespace std does not name a type
(10 answers)
Closed 9 years ago.
Simple program like this
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex
std::mutex mtx; // mutex for critical section
int main ()
{
return 0;
}
Tried the following to compile
$ /usr/local/Cellar/gcc46/4.6.4/bin/g++-4.6 -std=gnu++0x -I/usr/local/Cellar/gcc46/4.6.4/gcc/include/c++ -L/usr/local/Cellar/gcc46/4.6.4/gcc/lib temp_mutex.cpp
temp_mutex.cpp:6:1: error: 'mutex' in namespace 'std' does not name a type
By the way I am compiling on Mac.
It worked for me:
$ g++-4.7 -O2 -Wall -std=c++11 -c mutextest.cc
$ g++-4.6 -O2 -Wall -std=c++0x -c mutextest.cc
$ g++-4.6 -O2 -Wall -std=gnu++0x -c mutextest.cc
$ g++-4.4 -O2 -Wall -std=c++0x -c mutextest.cc
$ g++-4.4 -O2 -Wall -std=gnu++0x -c mutextest.cc
None of these generated any error messages.