0

I have a file where I'm trying to overload some operators and it's supposed to terminate early given some condition. This file is separate from the main file

#include "MY.h"
// this file is MY.cpp
// MY is a class

int& MY::operator[](int i) {
    if (/* condition is met */) exit(1);
    else {/* proceed... */}
}

If I compile that, it gives me this error

c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16' collect2.exe: error: ld returned 1 exit status

But let's say I have a main() with nothing in it, like so:

// same stuff as above code

int main() {}

The new code with an empty main() function will compile with no complaints.

Now, I have another file and the actual main() that I want to use is in this file:

#include "MY.cpp"

int main() { /* some code... */}

If I compile this file, it gives me this error:

error: redefinition of 'int main()'

The obvious way to fix this is to get rid of main() in MY.cpp, but doing that will give me the first error about a returned 1 exit status (don't even know what that means).

**So the bottom line is this: How can I terminate a function if that function is defined in another file that I'm including in my main file? **

Manuel
  • 2,143
  • 5
  • 20
  • 22
  • It's a linker error, it seems you didn't well configure your IDE... – Pierre Jun 15 '17 at 06:03
  • The first error comes from the fact that you are trying to compile the first file as if it was a full program, so it correctly complains that there's no `main`. The point is not to provide a `main`, but to compile it along with the other files - either you specify all the cpp files on the command line, or you compile them separately to object files (using the `-c` option) and link them together at the end. Generally IDEs will handle all this for you - just add the files to the project. Most importantly, you *don't* include cpp files - you only include headers. – Matteo Italia Jun 15 '17 at 06:06
  • Visual Studio lets you choose between console applications that get a console window and use `main`, and window applications that start without a console and use `WinMain` instead. You seem to want a console application but chose a window application. You can fix that in the project settings somewhere. – nwp Jun 15 '17 at 06:08
  • @MatteoItalia, Thank you for your answer, but then how do I make all the stuff I defined in my MY.cpp file visible in the second .cpp file, where I did #include "MY.cpp" ? – Manuel Jun 15 '17 at 06:14
  • You put declarations in the `.h` and definitions in the `.cpp`, then you include the `.h`. See https://stackoverflow.com/q/3730923/214671 – Matteo Italia Jun 15 '17 at 06:24

1 Answers1

2
#include "MY.cpp"

Immediate red flag. You should only #include header files, not cpp files. The #include directive inserts a copy of the entire file specified in its place. That means that if multiple source files in your project #include the same cpp file, anything defined in that file will be defined multiple times and will violate the One Definition Rule.

The way to use multiple source files in a project is to include their header files in whatever source or header files require them, and add all the cpp files to the project in your IDE and let it manage compilation of them, or if you're running GCC from the command line then you specify them all as command line arguments. E.g.

gcc main.cpp MY.cpp -o MY.exe

As for the error you are receiving, that suggests that there is no main function defined in any of your source files, which is possible if you are trying to compile MY.cpp to a complete executable, rather than to an object file, or rather than compiling it with a separate cpp file that defines a main function.

This sounds like a Windows issue, so it could also be that you are defining main when it expects WinMain instead. See this issue for help with that: Replacing WinMain() with main() function in Win32 programs

David Scarlett
  • 3,171
  • 2
  • 12
  • 28