__attribute__ ((warn_unused_result)) does not produce warnings for shared_ptr - gcc

Note: Added example below and updated title after comment about reproducibility.
We are trying to improve our codebase by supplying attributes to our functions.
on some functions we have added the attribute:
__attribute__ ((warn_unused_result))
At some points we deliberatly put some code that should produce a warning as we do not use the result of the function.
e.g.
shared_ptr<type> functionName() __attribute__ ((warn_unused_result));
functionName(); // this should produce a warning
However no warnings are produced. Are these warnings suppressed by default or are there some other dependencies to make this work.
I found some info here: https://www.linuxquestions.org/questions/programming-9/gcc-warn_unused_result-attribute-917158 via google, but this does not point me in a direction on why it does not work for non-standard features.
As four our setup:
g++ version 4.9.2
Debian version 8.6
enabled warning flags on build:
-Wall -Wextra -Wconversion -Wshadow -Weffc++
Here is an example that reproduces this issue:
#include <iostream>
#include <memory>
std::shared_ptr<std::string> function(int b) __attribute__ ((warn_unused_result));
int other(int b) __attribute__ ((warn_unused_result));
int main()
{
auto lResult = function(3); // not expect warning
std::cout << lResult->c_str() << " is the result" << std::endl;
std::cout << function(4)->c_str() << " is the result too." << std::endl; // not expect warning
function(5); // expect warning but this warning is not given.
auto lOther = other(3); // not expect warning
std::cout << lOther << " is the result" << std::endl;
std::cout << other(4) << " is the result too." << std::endl; // not expect warning
other(5); // expect warning and it is given.
return 0;
}
std::shared_ptr<std::string> function(int b)
{
auto lString = std::make_shared<std::string>("::" + std::to_string(b) + "::");
return lString;
}
int other(int b)
{
return 5 * b;
}
and CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(ShowCase)
add_definitions("-std=c++14")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Wall -Wextra -Wconversion -Wshadow -Weffc++")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -Wall -Wextra -Wconversion -Wshadow -Weffc++")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wall -Wextra -Wconversion -Wshadow -Weffc++ ")
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra -Wconversion -Wshadow -Weffc++")
add_executable(ShowCase main.cpp)
Any help or pointers would be much appreciated.
Regards,
Jan Jaap

This evidently is a compiler bug specific to C++ that is present until GCC 6.3
and fixed in GCC 7.1. Upgrading GCC would seem to be your
only solution.

Related

C++ template aliasing g++ 5.4

I'am not able to compile any trivial C++ aliasing declaration.
Here is my dev env :
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Here is my snippet (file tstFrameProd.cpp):
template<typename T = double>
struct mystruct {};
template<typename T = double> using myalias = mystruct<T>;
int main(int argc, char *argv[])
{
return 0;
}
Here is my compilation process :
g++ -g -std=c++11 -W -Wall -I../include -I../../../Toolbox/CShmRingBuf/ -I/opt/matrox_imaging/mil/include -Woverloaded-virtual -ansi -pipe -fno-for-scope -DGNU_GCC -DDEBUG -c -o tstFrameProd.o tstFrameProd.cpp
Here is my compilation error message :
tstFrameProd.cpp:20:31: error: expected unqualified-id before ‘using’
template<typename T = double> using myalias = mystruct<T>;
You're compiling with -ansi which negates the effect of -std=c++11. Simply remove it.

Undefined reference to sequence when trying to input a vector

This seems a simple but somehow the compile sends this error message which I'm not able understand thus correct my code.
This is a simplified version of what I did, just so it can appear the error for you:
Main.cpp
include "myfunction.h"
int main(){
std::vector<int> myVet = {1,4,3};
sequence(1,2,1,myVet);
}
myfunction.h
#include <vector>
/*funtion creates a sequence*/
void sequence(int start, int end,
int step, std::vector<int> skip);
myfunction.cpp
#include "myfunction.h"
void sequence(int start, int end,
int step, std::vector<int> skip){
auto x = 0;
};
This gives me an error message which says
In function 'main':
/home/machina/Documents/Grafos&Redes/Implementação/main.cpp:18: undefined reference to 'sequence(int, int, int, std::vector <int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status
Could you please explain me why it appears?
This is the following command which I've been using for compiling
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp -o main.out
You are only passing main.cpp to g++.
g++ needs to know about myfunction.cpp where your function is defined so as to compile and link it to your program.
The command to use should be:
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp myfunction.cpp -o main.out

tensorflow c++ error: Unary VariantShapeFn for type_name: int already registered

i compliled tensorflow with bazel successfully and got libtensorflow_cc.so and libtensorflow_framework.so. Then i built a c++ binary with those share libraries successfully, but errors occurred when i run the binary, info is like:
F tensorflow/core/framework/variant_op_registry.cc:51] Check failed: existing == nullptr (0x24fc538 vs. nullptr)Unary VariantShapeFn for type_name: int already registered
c++ code:
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
int main() {
using namespace tensorflow;
using namespace tensorflow::ops;
Scope root = Scope::NewRootScope();auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
// // Vector b = [3 5
auto b = Const(root, { {3.f, 5.f} });
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
std::vector<Tensor> outputs;
ClientSession session(root);
// Run and fetch v
TF_CHECK_OK(session.Run({v}, &outputs));
// Expect outputs[0] == [19; -3]
LOG(INFO) << outputs[0].matrix<float>();
return 0;
}
i got my c++ binary using cmd:
g++ -std=c++11 test.cpp -o test -I./include -L./lib -L/home/work/chengjy/tools/protobuf/lib -I/home/work/chengjy/tools/tensorflow/eigen/eigen/eigen-eigen-fd6845384b86 -I/home/work/chengjy/tools/protobuf/include -ltensorflow_cc -ltensorflow_framework -lprotobuf -lpthread -ldl -O3 -Wall
did anyone have this problem before?
it seems that i fixed this problem by removing -ltensorflow_framework from the compile cmd.

error: ‘defaultfloat’ is not a member of ‘std’

std::defaultfloat doesn't seem to be defined in GCC, despite being in the standard (I think it's §27.5.6.4). I've isolated it to this simple program:
// test.cpp
#include <iostream>
int main()
{
std::cout << std::defaultfloat << 1.3;
return 0;
}
This compiles in VC++11. I tried compiling this with g++ 4.7.2 and g++ 4.9.0 using both of these commands:
g++ test.cpp
g++ test.cpp -std=c++11
I also tried an online compile on GCC 4.8.1 here, always with the same result:
user#office-debian:~/Documents/test$ g++ test.cpp -std=c++11
test.cpp: In function ‘int main()’:
test.cpp:5:15: error: ‘defaultfloat’ is not a member of ‘std’
std::cout << std::defaultfloat << 1.3;
Why am I getting this error?
GCC libstdc++ just doesn't support these C++11 manipulators in any of
the versions you've compiled against. A patch was submitted exactly one month ago

How to use shared library

These are my C codes simply print “Hello" Message. And I want to make mylib.c as shared library.
[mylib.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
int mylib(){
printf("### Hello I am mylib #####\n");
return 0;
}
[drive.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
At the firest I compiled mylib.c with folowing command line to make mylib.o
gcc –fPIC –g –c –Wall mylib.c
Then tried to make it shared librarly like this
gcc -shared -Wl,-soname,libmylib.so.1 -o /opt/lib/libmylib.so.1.0.1 mylib.o -lc
And I did ldconfig to update /etc/ld.so.cache
Finaly I compiled drive.c link with mylib but linker showed error
gcc -g -Wall -Wextra -pedantic -I./ -L./ -o drive drive.c –lmylib
/usr/bin/ld: cannot find –lmylib
Dose someone tell me how can I compile it?
In my way, you have to follow some ways to use shared library in C.
At first I have created a header file named "shared_library.h", in this file I have introduced a function named "method" as a function of this library.
The code is following:
/*-------This is starting of shared_library.h file-----------*/
void method();
/*-------------This is ending of shared_library.h file--------*/
Then I have defined the method in another file named "shared_library.c". The definition as in code is:
/*-------------This is starting of shared_library.c file---------*/
#include "shared_library.h"
void method()
{
printf("Method is called");
}
/*-------------This is ending of shared_library.c file---------*/
And finally, the header "shared_library.h" is ready to use. I use the library in my main C file named "main.c". The contents of "main.c" are as follows:
/*-------------This is starting of main.c file----------------*/
#include <stdio.h>
#include "shared_library.h"
int main()
{
method();
return 0;
}
/*-------------This is ending of main.c file----------------\*/
I found this article ld cannot find an existing library.
It works if I change to gcc -g -Wall -Wextra -pedantic -I./ -L/opt/lib -o drive drive.c –l:libmylib.so.1

Resources