I'm making a program, and it's supposed to run a file in a different directory (/files/runme.c). How can I run this file in C?
I have tried the system() function, however this does not work.
MAIN.c:
#include <stdio.h>
#include <stdlib.h>
int main() {
system("runme.c");
return 0;
}
runme.c:
#include <stdio.h>
int runme() {
printf("hello world");
}
My expected result is:
hello world
I get:
exit status -1
I want it to run everything inside the contents of runme.c. How can I do this (on Windows and Linux)?
To get the runme() function from a different file to be passed to your main , you need to create a header file with the prototype of the runme() function in it, include this header file in main.c and compile using both files.
main.h:
int runme(void);
main.c
#include <stdio.h>
#include <stdlib.h>
#include "main.h" //main.h needs to be in the same directory as main.c
int main(void) {
runme();
return 0;
}
runme.c
#include <stdio.h>
int runme() {
printf("hello world");
}
Finally compile:
gcc main.c {path}/runme.c
from system() you can run .exe
as far as I know not .c.
Just compile the .c file you want to run put it on same folder as of file containing the main()
eg.
second_program.c->compiled->copy the generated .exe file to the same folder and then use the system(second_program.exe)
or you have to create two file second_program.h for header files and functions prototype etc
and second_program.c for their definition and then use include
if the files are not in the same folder as of main()
then you have to add the location of second_program in your program properties
or add include "second_program.h" if files exist in same folder
Related
In a CLion project, I have two C-language source files, "main.c" and "list.c".
The source file "main.c" has this:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
The source file "list.c" has this:
#include <stdio.h>
int printFoo() {
printf("I want Krabby Patties!\n");
return 0;
}
Now how do I call printFoo() from the main() function? I know I cannot do an include<list.c> in main.c since that will cause a multiple definitions error.
CLion uses CMake for organizing and building project.
CMakeLists.txt contains instructions for building.
Command add_executable(program main.c list.c) creates executable with files main.c and list.c. Add all source files to it. You can add headers, but it isn't necessary.
Header files contain definitions of functions and other things, source files for realization, but you can merge them.
main.c
#include "list.h"
int main() {
printFoo();
return 0;
}
list.h
#pragma once
int printFoo();
list.c
#include "list.h"
#include <stdio.h>
int printFoo(){
return printf("I want Krabby Patties!\n");
}
#pragma once tels compiler to include header file once. If you have more than one include of one file without #pragma once, you'll catch an error.
You can create one header file "list.h"
#ifndef __LIST_H__
#define __LIST_H__
int printFoo();
#endif
Then include it in main.c:
#include <stdio.h>
#include "list.h"
int main() {
printf("Hello, World!\n");
printFoo();
return 0;
}
I recently looked into defining functions using a header file in C.
I followed a tutorial online, but I have come across an issue:
If I use command prompt and run the executable file I created in my project folder, it takes the input from my main.c file and passes it through the function like I expected it to.
The command that I typed in Command Prompt was:
gcc matrix_product.c main.c
Within main I can call the function (which I named matrix_product), and it recognizes it. When I try to build it inside CodeBlocks, however, the compiler indicates 2 errors:
undefined reference to 'matrix_product'
error: ld returned 1 exit status
This is the code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "matrix_product.h"
#define N 3
int main()
{
int i,j;
int m[N][N]={
{1,2,3},
{4,5,6},
{7,8,9}
};
matrix_product(m,3);
for(i=0;i<N;i++){
if(i==1){printf("M^2 = ");
}else{printf(" ");}
for(j=0;j<N;j++){
printf("%d ",m[i][j]);
}
printf("\n");
}
return 0;
}
matrix_product.c
#include <stdio.h>
#include "matrix_product.h"
void matrix_product(int m[][3],int DIM)
{
int i,j,k;
int tmp[DIM][DIM];
for(i=0;i<DIM;i++){
for(j=0;j<DIM;j++){
tmp[i][j]=m[i][j];
m[i][j]=0;
}
}
for(i=0;i<DIM;i++){
for(j=0;j<DIM;j++){
for(k=0;k<DIM;k++){
m[i][j]+=tmp[i][k]*tmp[k][j];
}
}
}
}
matrix_product.h
#ifndef MATPROD
#define MATPROD
void matrix_product(int m[][3],int DIM);
#endif // MATPROD
main.c seems to be the only source file added to the project in CodeBlocks. Add the source file matrix_product.c to the project by using Project->Add files and it'll compile that too and link (ld) with the object file it produces.
Just having the file in the same folder does not make CodeBlocks understand that it should include the file in the project.
I have 3 .c files main.c, fun1.c, fun2.c
char buff[50];//in fun1.c
char *arg; //in fun2.c
arg = strstr(buff, "001"); //in fun2.c
I want to print buff in fun2.c but it gives an error buff undeclared, even though I declared it in fun1.h as extern char buff[];
There are functions in fun1.c and fun2.c each
It is hard to say what is wrong with your particular program, but here is an example which links 2 .c files with one .h file.
1. A header file functions.h:
#include <stdio.h>
extern void func();
Where I use extern to provide definitions for another file.
2. Now, a functions.c file which uses this header file:
#include "functions.h"
void func() {
printf("hello");
}
This needs to #include the header file, and use the function void() to print a message.
3. Finally, a main.c file which links it all together:
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main(void) {
func();
return 0;
}
Which also needs function.h as it uses func(). You then can compile the code as:
gcc -Wall -Wextra -g main.c functions.c -o main
You could also look into makefiles, which would reduce this long compilation line to simply make.
I am using qt and created a c project. I have created two files one header file and source file. I have declare a function into header file. So that I can call it from main function. But when I compile and run, I got "undefined reference to " error. How to solve this issue ? I am using qt 5.5 ide.
My Code:
header file
chapter_1.h
#ifndef CHAPTER_1_H
#define CHAPTER_1_H
//include all header files
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* function declaration */
int sum(int x, int y);
#endif // CHAPTER_1_H
source file
//include header files
#include "chapter_1.h"
int sum(int x, int y)
{
int result = x+y;
return result;
}
main file:
#include "chapter_1.h"
int main()
{
sum(23, 23);
return 0;
}
It's not a compiler error. It's a linker error. You just need to include both source files (main.cpp and chapter1.cpp) into your project.
I solved the problem. The problem was, file created as .cpp. Now I have changed it into .c and not it worked. Thanks to all.
You must tell the qmake which are the source files that you want to use to generate program. There is a couple of variables defined in project file ( *.pro) which are responsible for this and other information. SOURCES defines the source files to use, HEADERS - you guessed, the headers.
HEADERS = chapter_1.h
SOURCES = main.cpp chapter_1.cpp
I have those 3 files in my workspace:
1.h
#ifndef SOME_HEADER_FILE_H
#define SOME_HEADER_FILE_H
//code
#endif
1.c
#include <stdio.h>
#include "1.h"
//code
main.c
#include "1.h"
int main(){
printf("hello");
}
but in main.c printf is unidentified, is there a way to make it work while the relevant header is called in 1.c?
how can I link the 1.c and 1.h files?
edit: it's an academic assignment and I'm not allowed to make changes in the main and header.
You have included #include <stdio.h> only in 1.c, not in 1.h or main.c.
Obvious solution is to include it in main.c.
Because of the way the #include macro works (it expands the whole header file that you include at the line where you call the macro), you actually don't need to include stdio.h within main.c as long as stdio.h is included in a header file that main.c includes.
Hopefully this makes it clear:
main.c
#include "test.h"
int main()
{
printf("I can call this function thanks to test.h!\n");
return 0;
}
test.h
#include <stdio.h>
This will work just fine.
But this is not the same as being able to use a function that a .c file has access to based on its own #include definition just because you cross-compiled them. In that case the other.c file that calls #include <stdio.h> will get printf(), but main.c does not.
In this setup,
main.c
int main()
{
printf("I don't have any way to call this...\n");
return 0;
}
test.c
#include <stdio.h>
You will not have any way for main to know what printf() is, even if you cross-compile the two. test.c knows what printf() is but not main.
What you want is to have #include <stdio.h> in other.h, and then #include "other.h" in main.c.
But for future reference, this is probably poor practise as it should be immediately apparent what each file's requirements are so that you get a good sense of what its job is.
So here's what I would probably suggest as the best solution:
main.c
#include <stdio.h>
int main()
{
printf("I can call this all on my own.\n");
return 0;
}