-3

Every time I write C++, I need put different code in to main, is there any way make it simple, like a1.cpp has main, a2.cpp has another main, like in a1.cpp

int main() {
    printf("a1");
}

and in a2.cpp

int main() {
    printf("a2");
}

when you run a1.cpp you get a1 printout, when you run a2, you get string a2, how can i do it?

Mysticial
  • 464,885
  • 45
  • 335
  • 332
stackover
  • 122
  • 1
  • 7
  • 4
    Probably it's just because that's how the languages were designed. – Mysticial Dec 03 '12 at 23:58
  • related http://stackoverflow.com/questions/146576/why-is-the-java-main-method-static – sehe Dec 04 '12 at 00:02
  • You could obviously create a shared library instead of a standalone executable (on windows, it is actually the same thing) and create a runner (much like java/javaw) and make it select an entry point to start from the the shared library. No problem – sehe Dec 04 '12 at 00:04
  • Because the Java designers could handle the truth that everything, everything is an object, dammit. – Kerrek SB Dec 04 '12 at 00:07
  • Also, I'm really confused how the question title relates to the question body. At all. You can't "run a cpp file". And what's not simple about C++? – Kerrek SB Dec 04 '12 at 00:09

3 Answers3

5

Technicality: you can have as many main functions as you want in a C++ program, but only one with extern linkage in the global namespace.

If you want multiple entry points in a C++ program, the simplest is probably to use a command line argument, and have the primary main dispatch to other startup functions based on the argument.

One alternative is to build multiple executables, one for each startup function you want. You can select the startup function by linking with an object code file with that startup function. Or you can use preprocessor directives to conditionally include only the relevant definition of main for each build.

Another alternative, which however is system-specific, is to create a Windows DLL with multiple exported functions. A 32-bit such DLL can then be run via the rundll32.exe standard Windows program.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
3

It's valid as long as you don't link the object files together. As-is, the files a1.cpp, a2.cpp, a42.cpp are completely unrelated. You can compile them to separate executables, and execute them separately just fine.

The important aspect of this is that you don't "run" a1.cpp. You "run" a the output of the compiler after it has processed a1.cpp.

In C++, main - the entry point of the program - resides at global scope, as opposed to Java, where it's wrapped in a class. So you can't have multiple mains in a single program due to a multiple definition. The Java equivalent of this would be having multiple definitions of the same class (same name), which is also illegal.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

Well, I think main difference is that Java bytecode is executed JVM, which is executed on OS, while C++ programs is executed directly on OS, so basicly you can tell to JVM what is the entry point of an application, while you run on OS entry point is pre-deinfed by memory location (at lease from what I've know, someone correct me if I'm wrong), and thus can't be changed. Though you can easily write BASH script (for *NIX), or *.bat file on windows, that will take file name as an input, compile it, and than execute it. In bash that would look like:

build.sh:

#!/bin/bash
gcc $1 -o prog.out && ./prog.out && rm prog.out

.

./build.sh a1.cpp
./build.sh a2.cpp
toske
  • 1,744
  • 13
  • 24