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 "
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 two C programs:
Main.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("gcc x.c -o x");
system("x.exe");
return 0;
}
x.c
#include <stdio.h>
int main() {
printf("hello world!");
return 0;
}
From Main.c I need to compile and run x.c without using the command line. I've tried this but the code didn't work.
Thanks
This function call will compile your file and run it in one line :
system("gcc x.c -o x && x.exe");
I have source files written in C programming using notepad++ and I am running them from command lines and later i need to link them inorder to generate the .exe file.
Here are the following commands I want to use while generating .exe file
gcc logc.c -o logc
gcc mainc.c -o mainc
gcc -o output logc.o mainc.o
But when i run the following command my compiler is returning with the following error status.
gcc logc.c -o logc
(x86)/mingw-w64/i686-8.1.0-win32-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x39): undefined reference to `WinMain#16'
when i run the following command to compile my mainc file
C:\Users\user\AppData\Local\Temp\ccskY3nf.o:mainc.c:(.text+0x31): undefined reference to `Log'
collect2.exe: error: ld returned 1 exit status
And here are my mainc.c and logc.c and logc.h files for your reference
logc.c file is here
#include <stdio.h>
#include "logc.h"
void InitLog()
{
Log("Initializing Log");
}
void Log(const char* message)
{
printf(" %s",message);
}
mainc.c file is here
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#include "logc.h"
int main()
{
int x = 5;
bool comparisonResult = x == 5;
if(comparisonResult == 1)
Log("Hello World");
return 0;
}
and logc.h file is here
#ifndef _LOG_H
#define _LOG_H
void InitLog();
void Log(const char* message);
#endif
How can i compile individual source files and then link them and generate an executable file.
Thanks in advance.
You don't create object files, for that you need the -c argument:
gcc logc.c -c
gcc mainc.c -c
gcc -o output logc.o mainc.o
By default gcc will generate an executable file, not an object file. So when you compile logc.c, it tries to make an executable but it can't find the main function so it fails. Similarly with main.c, it tries to make an executable but can't find Log
You need to add the -c option to create object files:
gcc logc.c -c -o logc.o
gcc mainc.c -c -o mainc.o
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
I have a programe, which is so simple. The code as below.
I compiled it with
gcc -g -Wall -I../software/libxml2-2.9.0/include/
-lxml2 -L/usr/lib test.c -o test
I can absolutely run it with "./test", but when I run it with "gdb test" and then print "run" it will receive signal SIGSEGV. So I want to know what happened?
#include <libxml/xmlreader.h>
int main( void )
{
const char *file = "/usr/share/mime/application/javascript.xml";
xmlNewTextReaderFilename( file );
return 0;
}
If you are debugging using gdb test you may actually be debugging /bin/test rather that your own program. If the backtrace does not correspond to your expected program switch to gdb ./test (in a similar manner to how you are running the program as ./test)