Execute a C program from another program in gcc - c

I need to include a .h file to my project which will be supplied at the runtime. Since .h files are linked at linking time i am unable to include .h file. So i decided to write a dummy program which would create .h file and then i would call my actual program. Is there anyway to do this. Or any other solution is possible. I basically need to create a .h file before my program starts execution and need to link it up to my program.
i actually should take a file which is created by user, parse the file and then create a structure with the fields present in that file.for example if the file contains the following data:-
fno:int:4,fname:char:30,ftype:int:4
then i should create a structure like
struct somename
{
int fno;
char fname[30];
int ftype
};
Then i should be able to create instances of the structure created. This is what i like to do

dlopen is a solution. It allows to load dynamic library at runtime.
Compile your dummy program as a dynamic library.
Make use of dlopen on your .so
Call any function you need as if it has been linked by gcc (see dlsym).

What you can do is:
create .h file
fork
if in child: execve
if in father: wait (or not, depends on what you want to do)

I would use a Makefile; your program would receive the header file at runtime, (perhaps check it?) then execve() the make command passing the name of the file.
However, this sounds very cumbersome; perhaps you are trying to achieve something with the wrong tool. Maybe you want to use some scripting first? Or write two separate programs..? What are you trying to do?

Related

Combine two C program files in VScode, macOS

I got two C program files, a basic code
main.c and function.c
the function.c file contains the function which I need to call in main.c file
i've tried merging files using -o
the error appears to be : liker not found, although all the libraries and stuff are perfectly present in my system.
I'am facing this problem in vsCode in macOS.
Let's begin with taking an example in the attachment there are two pictures if you see the picture which has code for one.c there I have used a line
#include "two.c"
Just to tell the compiler that I want to see every single definition in two. c
In file one.c I have called a function named print which is defined in two. c
One thing that can be noticed and is a really important observation is that in two.c, the file from where definitions needed to be called doesn't have a main function. Can you guess why? The reason is simply because when you click on one.c your system will call the main function located in one.c. Upon seeing two main functions on the second file it gets confused and throws error, and just think why you even need the main function in two.c. There is no need because we use the main function just to perform certain actions and in two.c you really don't need to perform any action but you only need a function to be exported on another file.
In your case, you need to eliminate the main function from the function .c file and in main .c just include the path of your function .c

How do I bundle/include and use files into an executable in C - linux?

I've googled this for a bit but I don't seem to get anything resembling what I’m asking.
I'm creating a very simple C script which creates a bunch of template files (none of them are code or libraries or anything, they're just txt files), depending on an argument passed through the console, the way I was gonna resolve this is to just use fopen and fwrite, basically assembling the files line by line, replacing only the ones I need, but I figure there must be a way to bundle some files into the code so I can open and replace just what needs to change from case to case.
I imagine i could also create a couple of const char *file_text and that would do the trick, but I'd like to know if what I'm asking is possible should the need to use something harder to work with than text arise.
Problem is I can't seem to find how, to be clear, I want to do something like this on the console:
./Gen.sh ProjectName
And have Gen.sh be a self-contained file, with no need to keep the original templates around on the same folder.
You can use the linker to do such embedding
$ ld -r -b binary cat.png -o cat.o
The object file will have three symbols in it,
$ nm cat.o
_cat_start
_cat_end
_cat_size
To use them from C, declare some extern variables
extern const char cat_start;
extern const char cat_end;
extern const int cat_size;
Then you can link the generated object file ,the same can be used for text files

How do I link C source files together using headers

C code on the left, bash window on the right
I am trying to create a new .c file by putting #include "name.h" at the beginning of my file but as you can see I'm getting an error.
It's saying that it cannot find the include file. Am I not linking them together correctly using the bash commands?
You use the right commands, but you need to have a main() function somewhere in your code in order for your program to compile.
Also, you do not need to put #include "name.h" to create a .c (source) file, .h file (headers) are used to declare your functions so you can use them in other source files.

Frama-C Graph of Full File

I have been trying to use the pdg-dot plugin to help create a good graph of my software.The problem is different files don't have main so Frama-C complains. When I use the -main tag and specify a function to start it, it only creates a .dot file for the function and anything inside of it. Is there a way to make a .dot file of the entire .c file I have?
I don't think so. Each generated PDG represents one function only. But you can get the PDGs for all functions that are reachable from the entry point (main).

Including source files in C files

I am very new to C, so I apologize for this newby question.
I would like to use this source code in my project: http://base64.sourceforge.net/b64.c.
So, I include it in my test file:
#include <stdio.h>
#include "b64.c"
int main()
{
return 0;
}
However, main() is defined in b64.c as well, so upon compiling, I get:
test.c:4:5: error: redefinition of ‘main’
b64.c:495:5: note: previous definition of ‘main’ was here
test.c: In function ‘main’:
test.c:5:1: error: number of arguments doesn’t match prototype
b64.c:495:5: error: prototype declaration
What is the correct usage of this source file, or any? How do we correctly use it, or use functions defined in that file?
Edit: I understand that the problem is due to the duplicate definitions of main. I know there can only be one. My question is rather, won't every meaningful project need it's main method? Then why is there a main method defined in b64.c? Are we just supposed to delete this method from the source code? It seems odd that the source code doesn't just come ready to be included and used.
It is never a good idea to #include a C source file into your code. You can either copy the code from the other C source file into your code, or include the needed prototypes in your code and make a call to the functions, linking those after compiling them separately.
you should use one of the two main functions.
If you want a new main, write your main method in your file and remove it from the 'b64.c' file, if you want to use it from the 'b64.c' file remove your (empty) main.
If main is defined in b64.c either you cannot simply redefine it in your main source file.
What do you want is to use several functions from b64.c in your program. Delete the main function from it and create a header file where you protoype all of the functions in b64.c. After that, include this header file in your main program. Have a look at this short Wikipedia entry. It should give you an overview of the topic.
Besides this: It seems that you aren't very familar with C. Try out some tutorials and go on reading about C.
First of all, you must redeclare the .c file as a .h, and then you have to go through the source code and rename any duplicate methods or global variable names. The main method is the starting point of a program so there can only be one.
(Usualy you dont include .c files, only .h)
if you want the functions inside the "b64.c" you should delete the main function from "b54.c"!
A C application needs one main() function. Your file b64.c looks like a self-sufficient C program so you don't need your test.c. Just compile and run b64.c.
When you are including that source file you are getting 2 main() declaration which is incorrect. So you have redefined "main" in this case.
Including .c into another .c file doesn't make sense. C files compile to .obj files, which are linked by the linker into the executable code , so there is no need to include one .C file into another. Instead, you can make a .h file that lists the functions and include that .h file

Resources