954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Including files at run time

Hello,

I know this much that I want to include two files in my program. But can I decide which files to include at run time? Is this possible?

Thanks in advance,
-Jishnu.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

Cant you use ifndef-else-endif directives?

EDIT: Run time, i wonder you could.

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

The simple way is to just compile the whole code, then have some kind of decision in the code, like this.

int main ( ) {
  if ( something ) {
    program1();
  } else {
    program2();
  }
}


A more complicated way would be to write each separate module as a DLL, then at run-time you can choose which library instance to access.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Salem if that was possible, how can we include the .so file at run time?

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 
EDIT: Run time, i wonder you could.

Sorry, I could not understand what you mean.The simple way is to just compile the whole code, then have some kind of decision in the code, like this.

But inside the functions program1() and program2(), how can I include a file?

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

What do you mean by "include a file"?

Are you talking about source code?
Or binary data (pictures, music, etc.)?
Or just regular text (ASCII, RTF, etc.)?

How do you want it to be used at runtime? And what difference does it make whether you include one or the other?

Answer these three and we'll have an easier time answering you...

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

If you want to pass the name of the file to your program at runtime then just add the filename on the command line when you run your program.
c:\myprog.exe file1.txt <Enter>

Then your program can use the arguments to main() to get the filename

int main(int argc, char* argv[])
{
   char* filename = 0;
    if( argc > 1)
        filename = argv[1];
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Are you talking about including header files at run time ?

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

The files I want to include are header files. They contain definitions of some functions. I want to specify which definitions are to be used at run time.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

You can only have pre processor directives.
#if
#else

You can not include header file as such during runtime.
They contain definitions of some functions. I want to specify which definitions are to be used at run time.
Header files normally should contain declarations you are going to use in your application.
If you don't have appropriate declarations you are expecting "undeclared identifier"

There are other ways to include code at runtime like load dlls, but to answer your question you cannot include header file as such on the fly.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 
You can not include header file as such during runtime.

Aw, that makes me feel sad :(There are other ways to include code at runtime like load dlls, but to answer your question you cannot include header file as such on the fly.

If that is the case, then I'd appreciate if you list some methods to do it beyond the premices of C.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

You can build static libraries and dynamic libraries and load them in your application. However, it is more platform specific than language specific.

You can use COM and point to right implementation of the function (rather interface).

There are many ways to achieve an objective, You need to select one which suits best to you.
Aw, that makes me feel sad
Don't worry; Not only you but no one can include header files during run time :).
Just chuck the purpose; “#include” is a PREPROCESSOR directive. You would like to include header file even before you start compilation.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 
However, it is more platform specific than language specific.


I'd like to start off with the most language specific and the least platform specific way.Just chuck the purpose; “#include” is a PREPROCESSOR directive. You would like to include header file even before you start compilation.
In that case, I'd rather reframe my question to: How can I select which function to execute at run time, all alternatives being stored in different files ;)There are many ways to achieve an objective, You need to select one which suits best to you.
Suppose I want to search for such methods, what keywords should I use?

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

Use pure virtual functions (run time polymorphism C++).
Or call functions in if else or switch blocks.

Suppose I want to search for such methods, what keywords should I use?

You can not surch or enamurate all functions of a executable.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 
You can not surch or enamurate all functions of a executable.

You misunderstood me. I'm talking about methods to achieve what I want. I'm not using the word methods in the sense of functions.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

You can use component object modeling, runtime polymorphism, Pure virtual functions.

Dlls (Dynamicly linked library)

Thanks,
Prateek

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

I think it would help everyone to understand if you would provide some examples. Do you need functions to display information in different languages, such as English, German, French etc.? You create a language DLL for each language you want to support then install the desired language dll when you install the rest of the program. I'm certain you must have seen this before when you try to install something that other people have written.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Actually I've coded a common interface for a two-player fighting (strategy-based) game. Now, The strategy (technically speaking, A.I.) code has to be written by the players in separate files. I thought I'd include those two files at runtime (because there would be many strategy files, each of them numbered) in my main code to start the game.

Thank you all for your replies :).

As this approach won't work, I'd appreciate if you show me some another way of doing the same thing in C.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

Use DLLs like AD suggested. Instead of supplying a language it should supply an AI. The functions will be the same for each AI DLL, so the only variable will be the name of the DLL opened with LoadLibrary().

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

If this is turn-based, the cleanest way to do this is with pipes. Have the AI programs communicate over stdin and stdout, and have the main program fork processes and setup pipes and launch the AI programs with the pipes tied to stdin and stdout. That way (also) you don't have to worry about AI programs going and reading/writing to strange places in memory in an attempt to disrupt the opponent's AI.

sarehu
Posting Whiz in Training
289 posts since Oct 2007
Reputation Points: 98
Solved Threads: 22
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You