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.

Recommended Answers

All 28 Replies

Cant you use ifndef-else-endif directives?

EDIT: Run time, i wonder you could.

ssharish

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 if that was possible, how can we include the .so file at run time?

ssharish

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?

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.

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];
}

Are you talking about including header files at run time ?

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.

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.

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.

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.

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?

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.

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.

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

Dlls (Dynamicly linked library)

Thanks,
Prateek

commented: Thanks :) +2

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.

commented: Thanks :) +2

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.

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().

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.

You could do it something like that, but it introduces more problems than it solves:

fork() doesn't work on Windows. Even if it did, you would still need to set up some IPC to make things work right (that is, keep state, avoid excessive process loads and task switching, and I/O buffering, response time, and message configuration).

The simplest, and most common way to do it is to use DLLs. That's what they were designed for.

Windows provides protected memory accesses, so one DLL cannot easily gain access to another's process. Further, doing so would require source-code knowledge of the target DLL. Finally, a game will not function well if subprocesses don't behave (and try to disrupt their bretheren).


Another option, if you intend to have a "end-user programmable" AI, is to provide a basic language interpreter (embed Tcl or Python or somesuch), and execute each AI's code in a sandbox environment.

commented: Great help :) +2

I had a problem which is something similar to what you are having, on how to interface the AI routine with my Code. But in my case the AI reasoning part part was done LISP. So for example if an Agent wanted to move from a point A to Point B there could be different path, which basically depends upon environment.

The LISP routine would normally find the path for me, the output of that function (Which is nothing out the path) will be sent to my C code using sockets.

You could use sockets to communicate between routines if your wanted. Which makes it more simple, some thing similar to RPC or RPCgen which you are requesting for a routine to be called on the server and the output of that function would directed to the client to the client.

Some alternative for you

ssharish

commented: Great Help :) +2

@Duoas

You could do it something like that, but it introduces more problems than it solves:

fork() doesn't work on Windows. Even if it did, you would still need to set up some IPC to make things work right (that is, keep state, avoid excessive process loads and task switching, and I/O buffering, response time, and message configuration).

I agree.

Another option, if you intend to have a "end-user programmable" AI, is to provide a basic language interpreter (embed Tcl or Python or somesuch), and execute each AI's code in a sandbox environment.

I didn't understand this, Duoas. Please would you elaborate a bit? Terms like basic language interpreter and sandbox environment are completely new to me.

@ssharish2005

The LISP routine would normally find the path for me, the output of that function (Which is nothing out the path) will be sent to my C code using sockets.

Do you mean that I can send a value from a function written in one language to the main function written in another language using sockets? I'm new to the concept of sockets.

You could use sockets to communicate between routines if your wanted. Which makes it more simple, some thing similar to RPC or RPCgen which you are requesting for a routine to be called on the server and the output of that function would directed to the client to the client.

I don't know what is RPC and RPCgen . Are these related to sockets? From what you've given it seems it is related to networking.

Do you mean that I can send a value from a function written in one language to the main function written in another language using sockets? I'm new to the concept of sockets.

YES thats right, you call the routine which is written in a different language. But the calling procedure is language independent. It will vary from language to language. But, in LISP you have a inbuilt library which allows to do some socket manipulation.

I don't know what is RPC and RPCgen . Are these related to sockets? From what you've given it seems it is related to networking.

YES, it is something related to networking. But its is more often used in the UNIX world. I dunno about this on Windows though. I will have to research. RPC stand for Remote Procedure Call. So the name itself specific what it is suppose to do. You register all your routines on the server and the server will be waiting for the connection from the client and when once connected the client will request for a routine to be executed and the server will execute it and sends back the results to client across the network. Goggle "RPC Programming". This comes under a topic of Distributed Computing

ssharish

commented: Thank you once again :) +2

Do you mean that I can send a value from a function written in one language to the main function written in another language using sockets? I'm new to the concept of sockets.

That's how the internet works -- its all done with sockets. Two programs running on the same or different computers can communicate with each other via sockets. And as explained by ssjarish it doesn't matter what computer language the programs are written in as long as that language supports sockets.

commented: Thanks :) +2

And as explained by ssjarish it doesn't matter what computer language the programs are written in as long as that language supports sockets.

Do C and C++ support sockets? Are any special libraries required?

I didn't understand this, Duoas. Please would you elaborate a bit? Terms like basic language interpreter and sandbox environment are completely new to me.

For example, you could embed a Tcl interpreter in your program (TCL or Tool Command Language is a script language). Or, you could link a Python interpreter into your application.

In either case, you can extend the interpreted language itself to provide functionality specific to your application. Once done, the end-users could write their AI scripts in the embedded language (e.g. Tcl or Python), and your program will execute the end-user's code as needed.

The end-user's code would be a simple text file (just like all code files).

However, executing code you didn't write is a security concern, so you will need to implement the interpreter such that the end-user's code cannot do anything it shouldn't (like access files, or create sockets, etc.) Such an interpreter is said to exist in a sandbox --basically a controlled environment. Sand stays in; foreign material stays out.

Fortunately, this is very easy to do in Tcl and Python (and Scheme :-).


Socket programming is a pain in C/C++. If you were to embed Tcl or Python, you could use the embedded language's facilities to open and manage the socket. Both Tcl and Python make handling sockets trivial.


Whether you choose to do this or not you've got a lot of homework ahead of you. Good luck!

commented: Thanks, once again :) +2

Whether you choose to do this or not you've got a lot of homework ahead of you.

Yes, of course. These concepts are very much new to me and it will take quite a bit of time to have a decent grip over them. Thank you very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.