Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking multiple executables in C++ (Unix) 1

Status
Not open for further replies.

ToddT

Programmer
Feb 7, 2003
14
0
0
US
Can anyone tell me how to link multiple .exe files. I wrote a program that called to three different executables given certain situations. The three programs run fine, but in when I linked them, I get a multiple main() error message. I am using system() to call the other programs. Any help or suggestions?
 
You can link object files together to form executables. You can't link executables together; you'd have more than one main function, hence your errors.

But you said you're using "system()" to call the other programs. If so, what's the problem? You'd just have a program that looks like:

Code:
int main()
{
    if ( /* something */ )
    {
        system( "someprogram" );
    }
    else
    {
        system( "otherprogram" );
    }

    return 0;
}
 
That's what the program looks like, believe it or not. Does system() call the object or executable file? And if it is executable, with function calls object files?
 
system() just calls a shell to execute the command as a separate process. You don't need to link the other files in any way. If I write a program like this:

Code:
// when.cpp
#include <cstdlib>
int main()
{
    system( &quot;date&quot; );
}

All I have to do is compile &quot;when.cpp&quot; and run it. I don't have to go find the &quot;date&quot; executable (which is a standard UNIX utility, btw) and try to link it in with &quot;when.o&quot; or the &quot;when&quot; executable, I just run &quot;when&quot;. It calls &quot;date&quot; as a completely separate process. They are separate executables (one of which happens to call the other), and are unrelated.


I'm not sure what your question means... you can sorta say it &quot;calls&quot; the executable; &quot;creates a new process based on the executable&quot; is more correct. You can't &quot;call&quot; an object file in any way. The only thing you can do with an object file is to link it with more object files to create an executable.
 
Re Hello

My answers is in the other post about linking ...

The only thing I want to write more is that the word &quot;link&quot; as nothing to do with the system call ...

usually when you have only libraries (without main function) ... you compile with the -c option and you create an objetc file .o

all you sources are compile like that with -c option ... even the one containing the main function

then after you link all of them together with the linker.
ex : gcc -o one.o two.o three.o etc etc ...

this is what mean the word link of we are talking about the object file.

the system call is different ...

if you have a first program ... you compile it like

gcc -o first one.o two.o three.o etc ...

then you have a program call first

in you source code you call call a shell process which lunch first by writing

system(&quot;first&quot;);

but now it has nothing to do with link.


Hope it helps

lcout
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top