1

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

1
  • As Steve said - don't do this. :-) Commented Nov 10, 2010 at 14:23

3 Answers 3

3

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).
Sign up to request clarification or add additional context in comments.

2 Comments

+1 but also: don't do this. Solve the real problem instead of this crazy problem that you've created for yourself in an attempt to solve the real problem ;-)
I agree with Steve. dlopen is basically for plugins (I still don't get if that's what you're doing by the way)
2

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)

Comments

0

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?

2 Comments

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
@nikhil: consider using python and executing C code from within it if you're concerned about performances. Your approach is quite difficult, since you're basically trying to write a parser to check for the syntax and to allocate memory.. good luck avoiding errors there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.