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
Related
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?
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.
I want to make a program in c that compiles and runs another program. I wrote this code and it returns this:
robi#Robi:~$ ./comp test.c
gcc: fatal error: no input files
compilation terminated.
Error
I compile it like this:
gcc -Wall -o comp Compilare.c
and run it like this:
./comprun test.c
test.c is this
#include<stdio.h>
int main(){
printf("Ana are mere");
return 0;
}
The code:
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main(int argc,char* argv[]) {
char comanda[100];
strcpy(comanda,"gcc -o run");
strcat(comanda,argv[1]);
if(WEXITSTATUS(system(comanda) == 0))
execl("./run","./run",NULL);
printf("Error");
return 0;
}
How can I make it run without errors?
The issue is that the gcc command you invoke inside the program looks like this: gcc -o runtest.c, i.e., it attempts to compile no input to an output binary called runtest.c. You need a space at the end of your string literal: "gcc -o run" -> "gcc -o run "
EDIT found an issue, but it still needs to be solved it should be below in answers
My task is to write app based on existing files. test.c(main) randapi.c randapi.h(2 functions in here) and initapi.c(one function). "how can You use dynamic library as dynamic loaded library. Using eg9 (where i made a dynamic library and it worked fine) write app where this libraries will be attached dynamic"
here is my try with a makefile but terminal says that :failed to open when i come to run file using ./program
i have tried also version without attaching initapi.c but then it says initRand is unknown besides that make file clearly attached it
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#define ITERATIONS 1000000L
int main(int argc, char** argv)
{
long i;
long isum;
float fsum;
void *lib;
lib=dlopen("librandapi.so", RTLD_LAZY);
if (!lib)
{
printf("failed to open");
exit(1);
}
int (*getRand)(int);
float (*getSRand)();
void (*initRand)();
getRand=dlsym(lib,"getRand");
getSRand=dlsym(lib,"getSRand");
initRand=dlsym(lib,"initRand");
initRand();
isum = 0L;
for (i = 0 ; i < ITERATIONS ; i++) {
isum += ((*getRand)(10));
}
printf( "getRand() Average %d\n", (int)(isum / ITERATIONS) );
fsum = 0.0;
for (i = 0 ; i < ITERATIONS ; i++) {
fsum += ((*getSRand)());
}
printf( "getSRand() Average %f\n", (fsum / (float)ITERATIONS) );
dlclose(lib);
return 0;
}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
makefile
zad9: test.c
gcc -Wall -o zad9 test.c -ldl
librandapi.so: randapi.o initapi.o
gcc -shared -o librandapi.so randapi.o initapi.o
randapi.o: randapi.c randapi.h
gcc -c -Wall -fPIC randapi.c
initapi.o: initapi.c
gcc -c -Wall -fPIC initapi.c
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
zad9: test.c initapi.c
gcc -Wall -o zad9 test.c initapi.c -ldl
librandapi.so: randapi.o initapi.o
gcc -shared -o librandapi.so randapi.o
randapi.o: randapi.c randapi.h
gcc -c -Wall -fPIC randapi.c
Look at this line in man 3 dlopen:
If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) path‐name. Otherwise, the dynamic linker searches for the object as follows (see ld.so(8) for further details):
(and then a buch of rules that do not include the current directory nor the one where the executable is).
My guess is that you are copying librandapi.so to the current directory and that's why dlopen() cannot find it.
If that's the case, the solution is easy:
lib=dlopen("./librandapi.so", RTLD_LAZY);
I was looking here how to do static libraries using GCC, and the explanation is pretty clear (despise the fact I had to rewrite the factorial function): I have a function (fact.c), the header of the function (fact.h), and the main function (main.c), all of them in my home directory.
fact.h
int fact (int);
fact.c
int
fact (int f) {
if ( f == 0 )
return 1;
else
return (f * fact ( f - 1 ));
}
main.c
#include <stdio.h>
#include "fact.h"
int main(int argc, char *argv[])
{
printf("%d\n", fact(3));
return 0;
}
So I had first to generate the object file (phase 1)...
$ gcc -c fact.c -o fact.o
...then to generate the static library (phase 2)...
$ ar rcs libfact.a fact.o
...later to do the static library linking process (phase 3)...
$ gcc -static main.c -L. -lfact -o fact
...and finally run the program (phase 4 and final)
$ ./fact
My question is the following. Let's suppose my program will be so big that I had no alternative than put the headers in a header directory (/include/fact.h) and the static libraries will also be in another directory (/lib/libfact.a). In that case, how the compilation and/or the code of this program will change?
Edit/Problem Solved: First, the main.c was corrected in order to include a header in another directory called include. Remember that, in this case, both of the .c files are in the main directory.
main.c
#include <stdio.h>
#include "include/fact.h"
int main(int argc, char *argv[])
{
printf("%d\n", fact(3));
return 0;
}
Second, to generate the static library in another directory (phase 2), this is what I had done:
$ ar rcs lib/libfact.a fact.o
Here is your answer,
$ gcc -static main.c -L. -lfact -o fact
-L Add directory to the list of directories to be searched for -l
Its in the link that you gave. If you put the seach direction correctly and low search range, it will not be a problem. Otherwise it is not going to compile the code. Because code did not know where is the header.
You can add -I to specify include path(s).
gcc -I/include fact.c
gcc -I/include -static main.c -L/lib -lfact -o fact_main