I have a library with a C++ wrapper around auto-generated C code, built with CMake and gcc. When I compile it I get these warnings which I would like to inhibit:
src/ssp/autogenerated.c: In function ‘x1111’:
src/ssp/autogenerated.c:185:1: warning: implicit declaration of function ‘x1111’; did you mean ‘x1110’? [-Wimplicit-function-declaration]
185 | x1111();
| ^~~~~~~~~~
I should be able to inhibit these warnings with the -Wno-implicit-function-declaration warning option. I add that to my CMakeLists.txt like so:
file(GLOB SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/ssp/*.c src/ssp/*.cpp)
add_library(mylib SHARED ${SRCS})
target_compile_options(mylib PRIVATE "-Wno-implicit-function-declaration")
However, there is still a *.cpp source in there and so compiling gives me:
cc1plus: error: command-line option ‘-Wno-implicit-function-declaration’ is valid for C/ObjC but not for C++ [-Werror]
cc1plus: all warnings being treated as errors
Is there a way inhibit these warnings? I'm imagining it might be possible to apply -Wno-implicit-function-declaration to C sources only or to have it ignored by g++?
Apply the option only to C sources. See cmake generator expressions:
target_compile_options(mylib PRIVATE
$<$<COMPILE_LANGUAGE:C>:-Wno-implicit-function-declaration>
)
Related
I'm trying to build my program using gcc 5 with c++11 enabled.
My makefile calls gccmakedep which then complains about missing c++11 support:
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 \
I modified the line in the makefile to set the required option:
gccmakedep -- -std=gnu++11 -- $(SRCS)
It doesn't work. I get the same error. I think gccmakedep doesn't recognize the option.
How can I solve this?
I've got a third-party build script for a c project, and I'm trying to add a c++ source file to that project. I don't really want to make any changes in script itself because then I'll have to keep my changes up-to-date when that other project updates.
That script is not aware of c++, however it uses gcc and allows me to append extra CFLAGS to specific source files, so I was able to append -xc++ flag and successfully compile it. But I can't modify existing CFLAGS, which contain some flags that does not make sence for c++, so I'm getting the following warnings:
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
cc1plus: warning: command line option ‘-Wmissing-prototypes’ is valid for C/ObjC but not for C++
cc1plus: warning: command line option ‘-Wold-style-definition’ is valid for C/ObjC but not for C++
cc1plus: warning: command line option ‘-Wnested-externs’ is valid for C/ObjC but not for C++
Are there any gcc flags to disable this specific warning?
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.
After compiling my program with
$ cmake .
$ make
I get 5 errors of this form:
$ make
Scanning dependencies of target Project
[ 50%] Building CXX object CMakeFiles/myproj.dir/main.cpp.o
In file included from /path/to/my/proj/main.cpp:2:0:
/path/to/my/proj/library.hpp: In member function ‘virtual double MyProjClass::get_value(double, double) const’:
/path/to/my/proj/library.hpp:419:26: error: ‘sqrtf’ is not a member of ‘std’
const float F = (std::sqrtf(1.0f + 2.0f) - 1.0f) / 2;
^
Other errors include floorf not being a member of std.
Looking at this thread, it's because I'm not compiling with C++11, and looking at this thread thread, "[...] CMake will make sure the C++ compiler is invoked with the appropriate command line flags."
Well, my CMake is obviously not doing this. Why am I getting these errors, and how do I go about fixing them?
Edit: Here's my CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(Noise)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp vector.hpp noise.hpp)
add_executable(Noise ${SOURCE_FILES})
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
target_link_libraries(Noise ${SDL2_LIBRARIES})
You don't say what C++ compiler you are using but assuming it is g++ 5.x though 7, sqrtf
and floorf are not within the std namespace under -std=c++11, -std=c++14 or -std=c++1z. They are
simply in the global namespace, when cmath is included.
Will libraries in a makefile still be added to compilation when not specified in the preprocessor directives of the containing source files? The makefile in question is listed below, I refer lF77 and lI77 to the unspecified libraries. Thanks.
makefile:
composite: maincomp.c screenio.c cscreens.c turbine.c stat.c decide.c file.c\
sys.c dummy.c util.c cglobal.h composite.h cscreens.h f2c.h\
maincomp.h screenio.h turbine.h util.h makefile gcc maincomp.c screenio.c cscreens.c turbine.c stat.c decide.c util.c\
file.c dummy.c sys.c -I/usr/5include -L/home/boltoj/f2c/libF77\
-lF77 -L/home/boltoj/f2c/libI77 -lI77 -L/usr/5lib -lcurses -lm\
-o composite -g
Linker only links in libraries that resolve unresolved symbols. In other words, if a library doesn't provide any symbols an object file needs, that library doesn't get linked in.