GCC compiling with -pg doesn't produce binary needed for Gprof - c

I'm running into a little problem and require some assistance. I would like to run gprof on some OpenMP and MPI hybrid code as a part of my testing. I understand that Gprof relies on a binary file which is created when you compile GCC (or mpicc) with a -pg switch.
I have tried adding this switch and my compiling succeeds (as in no errors are reported), however, the binary file is not created, but the executable is created as normal. I have also tried doing this on much simpler code, which uses pthreads, with the same result.
Please examine the below and let me hear your thoughts.
gcc -pg --std=gnu99 -pthread -Wall -o pthreadsv0 pthreads.c
GCC compiling with -pg doesn't produce binary needed for Gprof.

I suspect that the binary file you mention is in fact the profile data file (gmon.out), and it is generated when you run your program (which has to be compiled with the -pg flag).
Just execute your program and see if a gmon.out file is there.

The Gprof information is created when you execute the program after you compile with the -pg option. Try running your program. (You're profiling (Gprof) the execution of the program.)
The -pg compile option adds the necessary logic to create the profiling information when the program is executed. Executing the program, several times if desired or needed, allows the 'instrumented' code to write the data describing the logic flow and timing to the gmon.out file.

I encountered the same problem. The problem arose because I was shutting the program's execution with Ctrl + C instead of a proper exit (closing the GUI window in my case).

Related

Is it possible to pass GCC arguments directly from C source code?

I want to be able to pass arguments to GCC from my C source code, something like this...
// pass the "-ggdb" argument to GCC (I know this won't work!)
#define GCC_DEBUG_ARG -ggdb
int main(void) {
return 0;
}
With this code I'd like to simply run gcc myfile.c which would really run gcc myfile.c -ggdb (as the "-ggdb" argument has been picked up from the C source code).
I'm not interested in using make with the CFLAGS environment variable, I just want to know if its possible to embed GCC options within C source code
What you want to do is not possible in general.
However, recent GCC (e.g. GCC 8 in end of 2018) accepts many options and some of them could be passed by function attributes or by function specific pragmas (However, they don't accept -g but do accept -O2).
Also, you can use -g in every compilation (with GCC, it is mixable with optimization flags such as -O2; so runtime performance won't suffer. Of course the -g will increase compile time and size of produced object file). Notice that (on Linux) the DWARF debug information is visible in the generated assembler file (e.g. try to compile your foo.c with gcc -Wall -g -O -S -fverbose-asm foo.c, look into the generated foo.s, and repeat by removing the -g)
I'd like to simply run gcc myfile.c
That is a very bad habit. You should run gcc -Wall -Wextra -g myfile.c -o myprog to get all warnings (you really want them) and debug info in your executable myprog. Read How to debug small programs before continuing coding your program.
I'm not interested in using make with the CFLAGS environment variable
But you really should. Using make or some other build automation tool (e.g. ninja, omake, rake, etc, etc....) is, in practice, the conventional and usual way of using GCC.
Alternatively, on Linux, write a tiny shell script doing the compilation (this is particularly worthwhile if your program is a single source file; for anything bigger, you really should use some build automation tool). At last, if you use emacs as your source code editor, you could add a few lines of comments (like at end of my manydl.c example) specifying Emacs file variables to tune the compilation (done from emacs)
If these conventions surprise you, read about the Unix philosophy then study -for inspiration- the source code of some existing free software (e.g. on github, gitlab, or in your favorite Linux distribution).
At last, GCC itself is a free software project (but a huge one of more than five millions lines of mostly C++ source code). So you can improve it the way you desire (if you follow its GPLv3+ license), after having studying somehow its source code. That would take you several months (or years) of work (because GCC is very complex to understand).
See also this answer to a related question.
You might also (but I recommend not to, because it is very confusing) play tricks with your PATH variable and have some directory there -e.g. $HOME/bin/, ahead of /usr/bin/ which contains /usr/bin/gcc, with your shell script named gcc; but don't do that, you'll be confused. Instead write some "generic" mygcc shell script which would run /usr/bin/gcc and add appropriate flags to it (I believe it is not worth the effort).

Please help me to compile this C program? [Linux]

I am using Linux now, and trying to compile this by gcc BUT.......
this is my truly simple code:
#include <stdio.h>
int main(){
printf("Hello world\n");
return 0;
}
and this is so much weird output:
./try.c: line 3: syntax error near unexpected token `('
./try.c: line 3: `int main(){'
why is it?
I have tried the right way to compile it, such:
gcc file_name.c -o file_name and other types of way of compiling
chmod +rwx file_name.c
./file_name.c
but still I got that result, why?
You have to compile the code first.
Follow these steps.
gcc try.c -o try.out
to compile the code. The -o option is given to give a custom name to the executable that will be produced.
Then, you can run it by typing
./try.out
To run the executable.
Be informed though, that there are a number of command line options that you can use to get the information about your code and add more functionality. See this page for more information.
You are trying to execute the .c file, remove the trailing .c from the name of the file you want to execute.
Like this:
gcc -Wall -Wextra -pedantic -Werror -o executable file_name.c
You should not need to make it executable, it should already be executable since the compiler will do that.
./executable
As you see, I've passed some parameters to gcc to let it help in diagnosing problems, sometimes these problems are caused by your lack of knowledge and some other times because you write code quickly and miss some details. So using them is good (although compilation is slower, but that doesn't matter if you have a good and fast machine, wehreas having issues in the code does matter).
The meaning of these flags are as follows
-Wall Enable all warnings. Really some are not enabled, but most are.
-Wextra Enable extra warnings.
-pedantic make the compiler pedantic, i.e. stick strictly to the desired (default for this version of gcc) standard.
-Werror Consider that warnings are errors.
Also, you could have guessed this if you see what the error says
./try.c: line 3: syntax error near unexpected token `('
./try.c: line 3: `int main(){'
as you can see the shell is trying to execute the source code as if it was a shell script, so you can immediately notice that this is not the executable file generated by gcc, and then you would notice the .c in the file name.
Try
gcc try.c
./a.out
Compiles the code and runs it. Please read the manual page for gcc and there are many delights to behold (extra checking etc)

Callgrind Anotate not working in OS X 10.10

i want ask you for some question what can be incorrect,
i write a program in C and translate it in
gcc -Wall -pedantic
and run
valgrind --simulate-cache=yes --tool=callgrind ./a.out
that create callgrind.out.[pid-number]
and if i run
callgrind_annotate callgrind.out.[pid] main.c
out will be
-- User-annotated source: main.c
No information has been collected for main.c
is it any way how to annotate code and calls of function for program optimalize tools ?
note
gcc -g - pg progrma.c
not working because Apple unsuported debug option on gcc and gprof is unsupported too. And KDE program don`t want run on Mac ...
Thanks everyone to helpful information how to solve it
You're compiling with gprof profiling information when you compile with -pg. valgrind doesn't actually need that data to do it's profiling, what it does need is the debug information.
Using valgrind-HEAD, I took a simple piece of code and compiled it without -g and got the same result as you - i.e. No information has been collected for main.c.
When I compiled with -g, I got useful information about main.c, even when I compiled with optimization I got useful information.
Long and short of it is that you need to compile with -g, not with -pg to get it to work with callgrind.

How can I pass two options to GCC when compiling my program?

I was doing a homework assignment and I wanted to use the <ncurses.h>. So the gcc filename.c -lncurses generates an assembler output and./a.out executes the code. In order to avoid ***stack smashing detected*** or segmentation fault error I also need to execute the gcc filename.c -fno-stack-protector. Is there any way through which I could execute both the commands on the terminal or the code could execute the -fno-stack-protector command?
ps : please go easy I am a complete noob :) :P
GCC can take multiple options on the command line. You should be able to pass any combination of -f and -l options, provided those options are compatible. You can get a great deal of information about the correct syntax for invoking GCC by running man gcc (or, on some systems, info gcc).
And, as I commented above, if you're getting a "stack smashing detected" error, the solution is not to pass -fno-stack-protector (which just disables the code GCC uses to detect these kind of bugs), but rather to fix the actual bug in your program that is causing the stack to get overwritten.

Need clarification about certain terms used to do with compiling C files

I am learning C with the GCC compiler and Geany (Arch Linux, if it makes a difference). However, I am seeing the words compile and build used interchangeably, both in Geany and on the internet. I am asking for clarification that the way I understand the compiling process is correct, because Googling it is just making me more confused.
Say I write a simple helloworld.c file:
#include <stdio.h>
int main(void)
{
printf("Hello world!");
return 0;
}
If I run gcc -c helloworld.c, the compiler produces a helloworld.o object file. Geany calls this process compilation and the compiler says Compilation finished successfully.
Now, if I run gcc -o helloworld helloworld.c, the compiler produces an executable file called helloworld and Geany calls it building. However, the compiler again says Compilation finished successfully.
I understand that the -c option produces an object file, and that multiple of these can be linked together with libraries to produce an executable file, but I am confused about which scenario is compilation and which is building.
Furthermore, if I had just one source file in the project, such as a single helloworld.c file, is gcc -o helloworld helloworld.c enough to turn the source code into an executable?
Thanks
To answer your 2nd question: yes, gcc -o myprog myprog.c is just fine. So is gcc -o myprog *.c or gcc -o myprog foo.c bar.c baz.c.
To answer your first question: technically speaking, there's no word as 'building' :) However, the word 'building' and 'compiling' can be used interchangeably to describe the whole process of producing a final executable from source code.
In a more precise context you would say there is:
preprocessing, when the preprocessor includes header files, expands macros, etc.
parsing, where the parser tokenizes the source text and produces a structured data model (a so-called Abstract Syntax Tree) of the program flow.
compiling or compilation, when a code generator traverses the AST and generates assembly code from it
assembling, when the compiler driver invokes an assembler program which turns the assembly text into binary object code and finally
linking or linkage, when the compiler driver invokes a linker to look up symbols in libraries, fill in missing addresses, etc.
So, strictly speaking, only the 3rd small step is the compilation; furthermore, using the GNU toolchain and make, people tend to call the first four steps (producing an object file from a .c source file) compilation as one.
More on all this here...
Compiling is generally thought of as turning source code into machine code, but not necessarily also linking the machine code to create a final executable. Building is a more general term to describe the whole process of creating the final executable. Building will involve both compiling and linking. If you aren't using the -c option, then the linking is done automatically, so this would be considered building, but compiling was also part of that process.
You may find the terms being used a bit loosely though.
When you compile a program, the compiler just checks if the file has any compile-time errors (such as syntax and semantic errors) in it. When you "build" the program, the compiler first checks for any errors in it, and then converts the source code into machine code (actual compilation) and creates an executable file in the process.
For your second question, yes gcc -o helloworld helloworld.c alone would be enough to "turn the source code into an executable".

Resources