views:

93

answers:

4

I want to combine two programs into a single executable. One is an open source program that has a rather complex project file, the other is one of mine with a much simpler structure. Because of the relative complexities of the project files I feel it would make most sense to start with the open source project and modify it to include my source files.

My intention is that my program should be in control and treat the other as a collection of utility functions.

I noticed that the open source program has a function called "main()" whereas mine has a "WinMain()". Presumably I have to tell the compiler to call my WinMain() first. How do I do that?

Is there anything else that I should be aware of in the merging process?

EDIT: Both programs are C++, not C.

EDIT: The open source program does very little io.

EDIT: The key thing I want to know is do I have to modify something in the project settings to say "this is a windows program using WinMain() as opposed to a console(?) program using main()" or will the compiler somehow work this out for itself.

UPDATE: The joint program is now up and running.

+2  A: 

If your program has WinMain(), it is presumably a Windows program, while the other is a command-line app. Depending on what the original apps actually do, this may make it difficult to merge them. E.g. if the other app uses standard input/output extensively, it is challenging to transform it into a Windows app. However, if it is simply a bunch of utility methods without side effects, you may simply add the code to your project and start using it.

You have to set up your project to generate a Windows executable, the call to your WinMain() function will then be arranged automatically by the linker.

Péter Török
The other program does very little io. I can modify the few cases where its trying to print.
Mick
A: 

main means you have a C or C++ program, WinMain means you have a Windows program, and the two can't be joined easily.

I would advice you start with your windows program in visual studio and add the methods/classes/functions that you need and that are in the other source files.

vulkanino
It's not more difficult than say merging two command-line programs, both of which expect to control I/O, command-line arguments, etc.
Ben Voigt
A: 

I noticed that the open source program has a function called "main()" whereas mine has a "WinMain()". Presumably I have to tell the compiler to call my WinMain() first. How do I do that?

No offense, but reading a book on programming (prefferably in C) first, would do a lot to more to help you than posting this here.

Good luck in your future endeavours.

Jas
[There is no need to learn C before learning C++.](http://stackoverflow.com/questions/598552/should-i-learn-c-before-learning-c/598555#598555)
Péter Török
*both* programs are C++
Mick
Why do I need to learn more C or C++? My problem is with visual studio (at least I think it is). What's SO for anyway if not for this kind of question?
Mick
@Peter, you're right, my point was that he needed to grasp some basics first. @Mick - Again, no offense, my point was that since you seem to lack basics of C or C++ programming (both use main() and WinMain()), it would be much, much more beneficial and easier for you tu use a book on basics of C++ and another one (Preferably a Petzold) on Windows programming, because you would learn about main()/WinMain() functions in the first few pages. Hope this clarifies my position
Jas
I notice that 70% of your answers get zero (or less) upticks.
Mick
What's that have to do with your question ?
Jas
Apparently though, this one did get one vote up, before you downvoted it because it was displaying your lack of basic knowledge. So sad :)
Jas
@jas. Anyone can say "read a book" in answer to just about any question. Why even bother to have SO. I have been a C programmer for 25 years (though admittedly am fairly new to C++). I just thought your answer was rather unhelpful.
Mick
I am so sad that you've actually taken this personally. Look man, if you have been programming for 25 years, you should have understood that I had nothing but the best intent in my answer. Your question CAN NOT be answered on SO because the answer takes more than a FEW LINES. But since you don't understand it, or won't understand it, then feel free to be angry about me. I didn't see anyone else downvote me here (had a +1 on this answer), which usually will happen on a wrong/sarcastic/ironic etc. answer very quickly. Get a grip.
Jas
@Jas: Actually your answer got two upticks, so there must have been someone else giving it a downtick too.
Mick
+1  A: 

If the command-line program's main function does required initialization, you'll want to rename it and call it from WinMain. If not, you can just remove it entirely. #if...#else...#endif would be good in either case.

The important thing is that there's no longer a function called main to be found by the linker.

Peter's answer addresses some other problems you might run into during the porting process that aren't directly related to main vs WinMain.

Ben Voigt