cgo doesn't include CXXFLAGS - c++11

I want to call C code in Golang:
// #cgo CFLAGS: -I/usr/include/c++/8.1.1/bits
// #cgo CXXFLAGS: -std=gnu++11
// #include "c++0x_warning.h"
import "C"
but get error:
In file included from ./main.go:5:
/usr/include/c++/8.1.1/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
So cgo doesn't use CXXFLAGS. I tried -std=c++11 and it doesn't work too. What I do wrong?
$ go version
go version go1.10.3 linux/amd64

Please refer to the following SO question: Difference between CPPFLAGS and CXXFLAGS in GNU Make to figure out which flags you really need in the context of your program.
If you are calling pure C code (and not C++ code), I don't think you will need CXX_FLAGS:
CPPFLAGS are supposed to be flags for the C PreProcessor; CXXFLAGS are flags for the C++ compiler.
You might also want to check your go env. If you really need this flag, you can try to compile your program using env CGO_CXXFLAGS="-std=c++11" go build <YOUR_CODE>.

Related

setting g++ mode to C++11

I am trying to build cmake source, which requires C++11.
The build halts and apparently the complaint is that C++11 is not detected. The g++ mode is actually set to -std=gnu++17
This is part of the console log
---------------------------------------------
CMake 3.18.20200919, Copyright 2000-2020 Kitware, Inc. and Contributors
Found GNU toolchain
C compiler on this system is: gcc
C++ compiler on this system is: g++ -std=gnu++17
Makefile processor on this system is: make
g++ has setenv
g++ has unsetenv
g++ does not have environ in stdlib.h
g++ has stl wstring
g++ has <ext/stdio_filebuf.h>
---------------------------------------------
g++ -std=gnu++17 -DCMAKE_BOOTSTRAP -DCMake_HAVE_CXX_MAKE_UNIQUE=1 -c $HOME/Apps/CMake-master/Source/cmAddCustomCommandCommand.cxx -o cmAddCustomCommandCommand.o
This is part of the error in the log file...
In file included from /usr/include/c++/5/unordered_map:35:0,
from cmake_bootstrap_11920_test.cxx:4:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
^
cmake_bootstrap_11920_test.cxx:7:2: error: #error "Compiler is not in a mode aware of C++11."
#error "Compiler is not in a mode aware of C++11."
^
cmake_bootstrap_11920_test.cxx:70:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
int Member = 1;
Looking around on the web, I noticed that C++11 is only available after gcc version 4.6.
I checked my version, and it seems to be above.
g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
I understand the -std=c++11 flag is used to enable the C++11 features in g++, but I don't seem to know what I am doing in this case.
I tried editing the CompileFlags.cmake file, but no change occurs.
I came upon this page which points to the cmake source I am using.
It says...
bootstrap: Require compiler mode aware of C++11
Some compilers have enough features enabled in their default modes to
pass our simple C++11 unique_ptr check but do not enable enough to build
CMake. Poison this case so that we choose one of the explicit `-std=`
options for such compilers.
Not sure what that means exactly.
How exactly do I change the g++ mode, to C++11, so that on running the bootstrap command, C++11 is used?
Or, in other words, how do I change std to point to C++11 (-std=c++11)?
First of all, you have g++ version 5.4.0 in your host PC installed, which is good, cause it means this is also supports the C++11, which you want to use.
To set it up, you could define it in your CMakeList.txt file:
set (CMAKE_CXX_STANDARD 11)
that should do the trick.
Please also check the documentation:
https://cmake.org/cmake/help/v3.1/variable/CMAKE_CXX_STANDARD.html
Usually, I would suggest to use the latest standard that you compiler is supporting (https://gcc.gnu.org/projects/cxx-status.html), cause you'll get also the latest features introduced in that standard. Exception for this rather in case you are working with legacy codes.

cc1: error: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C [-Werror]

I need the -std=c++11 flag for c++ modules when compile nginx. If I configure nginx with --with-cc-opt="-std=c++11" and then make. It gives me the error described in the title. How can I get it compile without modifing nginx source code or the compiler(for now it's gcc 4.8) version?
According to the documentation: "--with-cc-opt=parameters — sets additional parameters that will be added to the CFLAGS variable."
CFLAGS enables the addition of switches for the C compiler, while CXXFLAGS is meant to be used when invoking a C++ compiler.

g++ 4.8.* std::chrono Undeclared

std::chrono ought to be supported in g++ 4.8.*. However, when I try to compile using it using g++ 4.8.3, it cannot find various declarations. I am, of course, using -std=c++11.
For example this invocation (from an autogenerated file; that's why the -std appears twice):
g++-4.8 -g -msse2 -m64 <defines> <warnings> -std=c++11 -fexceptions -std=c++11 <includes'-path> -c <source-file.cpp> -o <out-path>
Produces this error:
<source-file, line>: error: ‘std::chrono::monotonic_clock’ has not been declared
I wasn't able to find very much that wasn't immediately a compiler version or missing -std=c++11. By inference from this, I shouldn't need anything else.
Question: what's wrong, how do I fix it?
There is no std::chrono::monotonic_clock in standard C++. There is a std::chrono::steady_clock, however.
In fairness to Microsoft - and burritos everywhere - there was a monotonic_clock in the working drafts during the development of C++11 which was replaced by steady_clock.
It seems to me that you could use code like this to determine whether you're using an old library implementation (that provides monotonic_clock but not steady_clock) or a new one (that provides steady_clock but possibly not monotonic_clock).
#if defined(__GLIBCXX__) && (__GLIBCXX__ < 20120322)
typedef std::chrono::monotonic_clock steady_clock;
#else
typedef std::chrono::steady_clock steady_clock;
#endif
The datestamp above corresponds to the libstdc++ shipped with GCC 4.7.0, according to GNU. I'd welcome any improvements or corrections to this code, for example to support libraries other than libstdc++.

How to compile OpenCV code using a Cuda shared library compiled using nvcc?

For a test I have written a code of matrix multiplication in C(cuda) and compiled it using nvcc to create shared library using following command.
nvcc -c MatMul.cu -o libmatmul.so
Then i wrote a OpenCV code in C and tried to compile with following command.
gcc ImgMul.c `pkg-config --cflags --libs opencv` -L. -L/usr/local/cuda/lib64 -I/usr/local/cuda/include -I. -lmatmul -lcudart -o ImgMul
and I am getting following error.
gputest.c:(.text+0x3f): undefined reference to `matmul'
Could anyone tell me how to include cuda libraries while compiling a code in gcc.
OS: Ubuntu
gcc : 4.4.0
The first point to make is that
nvcc -c MatMul.cu -o libmatmul.so
does not make a shared library, it just compiles to an object file. Shared libraries and object files are not at all the same thing.
That aside, the reason for the symbol not found error is C++ name mangling. Host code in CUDA source files is compiled using the host C++ compiler, not C. So symbol names in the host code emitted by the compiler are subject to name mangling. To get around this, the easiest way is to declare functions which you wish to call from plain C code using the extern "C" declarator (see here for a reasonable overview of the perils of C/C++ interoperability).

GCC not recognising standard header files when using swig and distutils

I'm trying to generate a python wrapper for a C++ library that I am putting together. I have just come across SWIG and am trying to use this in conjunction with distutils. I'm modifying someone elses code, so odd errors were to be expected but this one is just confusing.
I've managed to generate a c++ wrapper file with SWIG and am now attempting to run a modified version of setup.py in order to install the wrapper (which itself may or may not work, but I'll cross that bridge when it comes to it.) When doing this compiler errors pop up about inability to include header files. Specifically - string, ostream, sstream, map and vector. All of which are standard libraries, included as "include ".
The code itself compiles, but in trying to create a wrapper this way it does not.
I'm not entirely sure what information is relevant to this but this is how the extension is made:
## Extension definition
import os
wrapsrc = './project_rewrite_wrap.c'
incdir_src = os.path.abspath('../include/project')
incdir_build = os.path.abspath('../include/project')
libdir = os.path.abspath('../lib')
ext = Extension('_project_rewrite',
[wrapsrc],
include_dirs=[incdir_src, incdir_build],
library_dirs=[libdir, os.path.join(libdir,'.libs')],
libraries=['ProjectMain'])
The gcc command that is run is:
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/home/ben/Project/rewrite/include/Project -I/home/ben/Project/rewrite/include/Project -I/usr/include/python2.6 -c ./project_rewrite_wrap.c -o build/temp.linux-i686-2.6/./project_rewrite_wrap.o
Which results in errors such as:
./project_rewrite_wrap.c:2696:18: error: string: No such file or directory
Any thoughts would be greatly appreciated, thanks.
You are compiling C code - the headers you mention are part of C++, not C. To compile as C++ code, use the g++ driver instead of gcc, and give the source files a .cpp extension instead of .c.

Resources