No warning in case of no initialization despite -Wall in gcc - gcc

the following code does'nt produce any warning when compiled with "-Wall" option in gcc:
int main ()
{
int c, i;
for ( ; i < 10; i++ ) {
c += i;
}
return c;
}
This is the command used to build the source:
$ gcc -c -Wall 1.c
$
It returns without any message.
I would expect a "warning: ‘i’ is used uninitialized in this function", and the same warning for the 'c' variable.
Any idea about this behavior?
Thank you.

Analysis performed by -Wall is very limited without optimizations. Warning is successfully detected with -O1 or -O2.

Related

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.

Why is the specific warning violation from gcc not shown with -std=c11/c99 on this code?

If I compile this program
main() {}
with
gcc -Wall -Wextra -Wpedantic smallest_program.c
gcc says:
smallest_program.c:2:1: warning: return type defaults to ‘int’ [-Wreturn-type]
main() { }
^
smallest_program.c: In function ‘main’:
smallest_program.c:2:1: warning: control reaches end of non-void function [-Wreturn-type]
main() { }
If I add C11 or C99 as the standard:
gcc -std=c11 -Wall -Wextra -Wpedantic smallest_program.c
the warning is:
smallest_program.c:2:1: warning: return type defaults to ‘int’
main() { }
I appears that C11 C99 has added an implicit return statement in main if you omit it. But why does the warning for the missing return type no long contain the specific warning that was violated: [-Wreturn-type] ?
This is from gcc (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
Because the implicit int rule has been dropped, so it is not a question of what warning flags are on but under C11 this must give a diagnostic, with or without warning options. And as a consequence, you can't silence that diagnostic.
Concerning the difference in the warning message about the implicit int return, implicit int is not allowed in C11 (IIRC, since C99), so the warning will always appear, regardless of what warnings/errors you enable (it is "enabled by default".) In other words, you don't have to be asked to be warned, because the implementation has to give you a diagnostic.
Concerning the control reaching the end of a non-void function, C has has an implicit return 0; in the main function since C99. That is why compiling in C11 mode does not produce a warning.
Because you have missed void at the start of main - that will default to an int - as it also assumes an int as an passed in parameter as there's no void there too.
I would suggest you try the following for main ...
int main( void )
{
return( 0 );
}
I am old, maybe things have changed. :-)

How to inspect the variables of user space functions in systemtap?

I met a problem when inspecting the local variables of user space application in systemtap.
I write a test.c like this:
#include <stdio.h>
int func(int *p, int val)
{
printf("p=%p val=%d\n", p, val);
return 1;
}
int main()
{
int a = 7;
func(&a, a);
return 0;
}
and compile it with -g
# gcc -g -o test test.c
Systemtap can see the variable of func(): p and val
# stap -L 'process("./test").function("func")'
process("/home/ryan/Public/test").function("func#/home/ryan/Public/test.c:3") $p:int* $val:int
So I use this stp to watch the variables:
# stap -e 'probe process("./test").function("func") {printf("%s(%p, %d)\n", probefunc(), $p, $val)}'
But the local variables are not right in the result when test program executed, it shows:
func(0x0, 0)
I am using fedora19 with:
kernel-3.11.9-200.fc19.x86_64
systemtap-sdt-devel-2.3-1.fc19.x86_64
systemtap-2.3-1.fc19.x86_64
systemtap-client-2.3-1.fc19.x86_64
systemtap-devel-2.3-1.fc19.x86_64
systemtap-runtime-2.3-1.fc19.x86_64
gcc-4.8.2-7.fc19.x86_64
Could someone meet this problem or give me a solution?
.function probes are defined to fire at entry to the function. If you're looking for values of local variables, you need to use .statement probes, identifying the source-file:line-number. In this case though, you're looking for parameters to a function (which happened to be based on another function's locals). In this case, the .function probe is appropriate.
You appear to be hitting a GCC bug. In plain -g mode (ironically), dwarf debuginfo is sometimes inaccurate for incoming function parameters. Try "gcc -g -O" or "gcc -g -O2" instead. Systemtap prologue-searching (stap -P) might help. See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51358, https://sourceware.org/bugzilla/show_bug.cgi?id=13420
In case the "stap -P" doesn't help, you may need to resort to statement-level probing after all:
probe process("./test").statement("func#test.c:5") { println($$parms) }
(line :5 refers to the printf)

Display kernel error

I'm using GCC and the NVIDIA implementation of OpenCL, and online compilation instead of offline compilation.
I use this list to check which is the error I have. But nevertheless if I have an error inside my kernel the only information I have is an error value -48.
My question is: Is there a way to display the exact kernel compilation error?
If a semicolon is missing, or I have a wild pointer I would like to read so, instead of just a -48 error. Otherwise the development time is getting too slow.
I add also my Makefile:
CC=gcc
FILE=main
all:
$(CC) -c -Wall -I /usr/local/cuda/include/ $(FILE).c -o $(FILE).o
$(CC) $(FILE).o -o $(FILE) -L /usr/local/cuda/lib64/ -l OpenCL
clean:
$(RM) $(FILE) $(FILE).o
In C++, do something like:
int ErrorCode = 0;
cl_program P;
cl_device_id D;
size_t LogSize;
cl_build_status BuildStatus;
//Configure OpenCL
//Load Program Here
//Compile Program here
//Check the status of compilation
ErrorCode = clGetProgramBuildInfo(P, D, CL_PROGRAM_BUILD_STATUS, NULL, NULL, &BuildStatus);
if(BuildStatus == CL_BUILD_ERROR){
//Fetch Error
ErrorCode = clGetProgramBuildInfo(P, D, CL_PROGRAM_BUILD_LOG, NULL, NULL, &LogSize);
char Log = new Log[LogSize]; //Or use Malloc if in C
ErrorCode = clGetProgramBuildInfo(P, D, CL_PROGRAM_BUILD_LOG, LogSize, Log, NULL);
//Display Error Code here, close out OpenCL, try again, etc
}

Resources