GCC not warning although it should - c

I have a very simple Hello World program, that doesn't have a return at the end of its main() function. If I understand correctly, this should throw a Wreturn-type warning, but when I compile it, no output at all is given. It simply compiles it and is done.
program:
#include <stdio.h>
int main() {
printf("Hello World!\n");
}
compilation commmand:
gcc -Wall -Wextra -o hello.o hello.c
I also tried specifically with the Wreturn-type option.
On Manjaro 18.0.0 with GCC 8.2.1

Use
gcc -std=c89 -pedantic ...
because in C99 1 main() does not need a return 0;. It's as if there was one right before the closing brace.

Related

A simple Hello World written in C does not output anything when using external library function

Actualy, i have to debug an old C program. I'm a noob at C programming and i'm facing a strange behaviour when running this program. This program use PCRE, a regular expression library ported to Windows (i got the "developer files" from http://gnuwin32.sourceforge.net/downlinks/pcre-lib-zip.php) but when the program try to use a function from this library the main method isn't even called.
I've narrowed it down to a simple HelloWorld as you can see below:
#include <stdio.h>
#include <regex.h>
int main(int argc, char *argv[]) {
printf("Hello, world!\n");
regex_t re;
regcomp(&re,"sam", 0);
return 0;
}
This program compile and link successfuly.
19:55:32 **** Rebuild of configuration Debug for project Test ****
Info: Internal Builder is used for build
gcc "-ID:\\Cpp\\pcre\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o Hello.o "..\\Hello.c"
gcc "-LD:\\Cpp\\pcre\\lib" -o Test.exe Hello.o -lpcre -lpcreposix
19:55:35 Build Finished. 0 errors, 0 warnings. (took 3s.750ms)
When executed it returns 0 and nothing is printed even if the printf call is the first call of the main function
If i comment the call to regcomp:
#include <stdio.h>
#include <regex.h>
int main(int argc, char *argv[]) {
printf("Hello, world!\n");
regex_t re;
//regcomp(&re,"sam", 0);
return 0;
}
Then compile and link
19:57:19 **** Rebuild of configuration Debug for project Test ****
Info: Internal Builder is used for build
gcc "-ID:\\Cpp\\pcre\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o Hello.o "..\\Hello.c"
..\Hello.c: In function 'main':
..\Hello.c:12:10: warning: unused variable 're' [-Wunused-variable]
12 | regex_t re;
| ^~
gcc "-LD:\\Cpp\\pcre\\lib" -o Test.exe Hello.o -lpcre -lpcreposix
19:57:21 Build Finished. 0 errors, 1 warnings. (took 2s.400ms)
When running the program it returns 0 and print as expected:
Hello, world!
Can someone help me understand what happen under the hood and successfully execute this regcomp function?

Error when using the powf() function in C

I wrote this C code to find the value for 3 squared.
#include <stdio.h>
#include <math.h>
int main( void )
{
float a;
a = powf( 3, 2 );
printf( "\n%f\n", a );
return 0;
}
I get the warning implicit declaration of function 'powf' even with math.h library included and -lm in the terminal command:
gcc -o test.exe -ansi -Wall test.c -lm
My gcc version is 4.2.2 if it helps.
powf is added in C99. Option -ansi is equivalent to -std=c89. You need to use -std=c99 flag.
gcc -o test.exe -std=c99 -Wall test.c -lm
The problem is the -ansi parameter. This is equivalent to -std=c90.
As the man page for powf states, you need to use -std=c99

how to run a code that accepts parameters on eclipse and bash?

i have this simple code that accepts numbers from the standard input and print them , i wrote this code on code blocks and it works .. now i want to run the same code on eclipse and i don't know how it's supposed to work ? also after that i run it on eclipse i need to run it on bash where i have a directory that includes tests and i nee to check my code with these tests but i can't figure how to compile this c program there !
this is this is the simple code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
int i;
int k;
int a;
printf("Enter size of input:\n");
scanf("%d",&x);
if (x<0){
printf("Invalid size\n");
return 0;
}
int *numbers=malloc(sizeof(int)*x);
printf("Enter numbers:\n");
for(i=0;i<x;++i){
scanf("%d",&numbers[i]);
}
for(k=0;k<x;++k)
{
a=numbers[k];
printf("The number %d is a power of 2 \n",a);
}
return 0;
}
also i am tried to compile this code on bash with this line :
-std=c99 -Wall -pedantic-errors -Werror -DNDEBUG main.c compiled.o
what am i doing wrong ?
Use the following command. Works like a charm in ubuntu bash. You can just input the values in the terminal, after running the program.
gcc main.c -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG -o main
Above a command generates a binary with the name main, Run the main file using following command.
./main
Then enter the your values.
To compile gcc main.c -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG -o main
to run from bash and accept arguments from a file named 'testcasefile' ;type following
main < (path to file)/testcasefile. 3 as for howv to run from eclipse refer to
https://stackoverflow.com/a/16921891/6721448
Let's start anew. Make a new directory for your project main inside project directory a directory for test case testcase
mkdir -p main main/testcase
now make test cases
Test case 1
2
3
4
compile main.c as follows
gcc - std=c99 -Wall -o main main.c
execute out put main with test case
./ main < testcase/testcase1

clang: error: linker command failed with exit code 1 (use -v to see invocation) on OS X [duplicate]

I compile this code main.c in CentOS7 with gcc:
#include <pthread.h>
void* mystart(void* arg)
{
pthread_yield();
return(0);
}
int main(void)
{
pthread_t pid;
pthread_create(&pid, 0, mystart, 0);
return(0);
}
1st compile: gcc -Wall -g main.c -pthread -o a.out
It's all OK.
2nd compile: gcc -Wall -g main.c -lpthread -o a.out
Gives
warning: implicit declaration of function 'pthread_yield' [-Wimplicit-function-declaration]
Can the 2nd a.out still run correctly ?
How to fix the warning without -pthread? Is sched_yield another way to yield a pthread ?
pthread_yield() is a non-standard function which is typically enabled by defining
#define _GNU_SOURCE
While you should use -pthread for compiling, I would expect you to get the same warning with both compilations (unless -pthread defines _GNU_SOURCE which may be the case).
The correct way to fix is to not use the non-standard function pthread_yield() and use the POSIX function sched_yield() instead by including #include <sched.h>.
You should use -pthread for compile and link. It not only links the library, it also sets preprocessor defines and sometimes selects a different runtime library (on Windows for example).

compiler isn't issue error/warning in mismatch function parameter

I have the next code :
test.c
#include "a1.h"
int main() {
int a = 8;
foo(a);
return a;
}
a1.h
void foo (int a);
a1.c
int f = 0;
void foo (int a, int b){
f=5+a+b;
return;
}
Pay attention that in a1.c foo has 1 more parameter than the prototype defined in a1.h.
The compiler isn't issue a warning or an error and so as coverity :
make all
Building file: ../src/a1.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/a1.d" -MT"src/a1.d" -o "src/a1.o" "../src/a1.c"
Finished building: ../src/a1.c
Building file: ../src/test.c
Invoking: GCC C++ Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/test.d" -MT"src/test.d" -o "src/test.o" "../src/test.c"
Finished building: ../src/test.c
Building target: test
Invoking: GCC C++ Linker
gcc -o "test" ./src/a1.o ./src/test.o
Finished building target: test
How can I defend myself in those cases ? I know that if I will add #include "a1.h" in the a1.c file I will get an error but is there a way to get an error without the "include " ?
Compiler isn't issuing a warning because it does not know that foo(int) from a1.h header and foo(int,int) from a1.c file is the same function. C++ allows functions to be overloaded, so both functions could potentially coexist. That is why C++ compiler cannot detect this problem, so you need to wait until the linking stage.
If you were compiling using C, not C++, you could have the compiler detect this condition simply by including a1.h at the top of a1.c file.
You're overloading foo. The version with only one parameter is never defined, hence you should get a linker error when using it.
How can I defend myself in those cases ?
You can't defend yourself from function overloading. Just make sure that you've got the same signature in both the header as the source file.

Resources