Multiple definitions of "Main" - c++

In the journey to learning C++ im learning through the C++ Manual thats on the actual website. Im using DevC++ and have hit a problem, not knowing whether its the compilers error or not.
I was going through this code bit by bit typing it in myself, as I feel its more productive, and adding my own stuff that ive learnt to the examples, then I get to initialising variables. This is the code that is in the C++ manual
#include <iostream>
using namespace std;
int main ()
{
int a=5; // initial value = 5
int b(2); // initial value = 2
int result; // initial value undetermined
a = a + 3;
result = a - b;
cout << result;
return 0;
}
This is popping up a compiler error saying " Multiple definitions of "Main""
Now This is on the actual C++ page so im guessing its a compiler error.
Could someone please point me in the right direction as to why this is happening and what is the cause for this error.

Multiple definitions of "main" suggests that you have another definition of main. Perhaps in another .c or .cpp file in your project. You can only have one function with the same name and signature (parameter types). Also, main is very special so you can only have one main function that can be used as the entry point (has either no parameters, one int, or an int and a char**) in your project.
P.S. Technically this is a linker error. It's a subtle difference, but basically it's complaining that the linker can't determine which function should be the entry point, because there's more than one definition with the same name.

Found I had two file references in my tasks.json file that were causing this error and which took me a long time to figure out. Hope this helps someone else..... See "HERE*****" below:
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
//"${file}", //HERE**********************
"-lgtk-3",
"-lgdk-3",
"-lpangocairo-1.0",
"-lpango-1.0",
"-lharfbuzz",
"-latk-1.0",
"-lcairo-gobject",
"-lcairo",
"-lgdk_pixbuf-2.0",
"-lgio-2.0",
"-lgobject-2.0",
"-lglib-2.0",
"-o",
"${fileDirname}/${fileBasenameNoExtension}" //HERE*************
],

When I practiced CMake, I encountered the same problem. Finally, I found that the source code path set in the cmakelist project was incorrect. As a result, the compiled files included many duplicate files generated during CMake execution. As a result, compilation errors occurred

Related

Linker removes unreferenced code

I'm currently working on a modular project and some of our systems won't work, I'll try to explain what we're trying to do.
We've got a main project that is extended by a number of modules (dlls), these modules can have bootstrapper code that will register itself before the main loop starts. It will register itself using our bootstrapping system, however the problem for us is that the modules will not register itself unless we specifically call a function from said modules. We think this is because the linker removes unreferenced code as part of optimizations (this also happens in debug mode).
The main function is set up as follows:
#include <bootstrap/bootstrapper.hpp>
#include <module/module.hpp>
int main()
{
// foo is an empty function in the module header file
foo(); // if I were to remove this empty function,
// the modules bootstrapping code will not execute
return fade::bootstrap::run();
}
without the foo() function the bootstrapping code doesn't get executed
void foo()
{
}
namespace
{
std::unique_ptr<game> game_;
FADE_BOOTSTRAP_MODULE(module_game) // registers itself to the bootstrapper
}
We've tried a number of things such as the linker options:
/INCLUDE
/OPT:NOREF
/EXPORT
But to no avail, either it gives us undefined symbol errors or it does nothing at all.
Is there anything we can do so that the unreferenced code doesn't get optimized away? We want to keep our project modular and cross platform so we'd rather not hardcode any solutions to our main function.

IDL CALL_EXTERNAL crashing when passing arguments to C++

I'm trying to run a c++ script from IDL using the CALL_EXTERNAL function. I've been able to get it to work without arguments, but when I try to add an arg, such as a single IDL LONG INT, IDL crashes. with the error:
% CALL_EXTERNAL: Error loading sharable executable.
Symbol: main, File = /home/inspired/workspace/TestCode/main.
so
/home/inspired/workspace/TestCode/main.so: wrong ELF class:
ELFCLASS64
% Execution halted at: TEST_EXTERNAL 7
/home/inspired/IDLWorkspace/Analyze Data/test_external.pro
% $MAIN$
The test code I'm using is as follows.
The C++ code:
#include <iostream>
int main(int argc, char *argv[]) {
int temp = (int) strtod(argv[1], NULL);
std:cout<<temp;
return temp;
}
The IDL code:
pro test_external
c= call_external('/home/inspired/workspace/TestCode/main.so','main', long(2), /AUTO_GLUE)
print,c
end
This code is of course practice code, but if I can't get this to work, then there's no way I'll be able to pass a mixture of arrays, and values.
I am aware that IDL passes everything by reference unless stated otherwise. So I've tried both treating the passed argument as a pointer in the C++ code, and setting the /ALL_VALUE keyword to pass the arg as a value. Neither works resulting in the same error as above. I've read about "glue functions" but I have not been able to find a guide to making them (despite every source indicating that it's 'easy for most programmers'" >.>
Anyway, my options are as follows, and if you can help me with any, I'd be eternally grateful:
Get this CALL_EXTERNAL function to work
Have the C code grab the data it needs from memory somehow
Rewrite everything in C++ (you don't need to help with this one)
Thanks in advance.
I think you are trying to mix 32-bit and 64-bit code. It looks like you are compiling your code as 64-bit, but you are running 32-bit IDL. To check this, IDL prints it when it launches or you can check manually:
IDL> print, !version.memory_bits
64

Arbitrary precision package

I'm having trouble with this arbitrary precision package.
I included "precisioncore.cpp", declared an int_precision, tried to compile and it told me that stdafx.h was missing.
I already read that I can simply omit this include in precisioncore.cpp, and so I did. After that it complained about memcpy not being declared in this scope, so I inlcuded .
The next error I cannot rectify:
\precisioncore.cpp|4222|error: call of overloaded 'int_precision(float_precision&)' is ambiguous|
This is line 4222: r2=(int_precision)rf;
r2 being an int_precision and rf being a float_precision. I understand that the float is explicitly casted into an int, but looking into the reference that came with the package this should not be a problem, at least not syntax-wise.
Does anyone here know this package? Any experiences with the same issue, maybe?
EDIT: It looks like the package is working perfectly in Visual Studio. Couldn't figure how to get it working in C:B, though...
ok so... I had the same problem while trying to hook up that library to CodeBlocks under GCC.
It seems to me that *int_precision(float_precision&)* constructor is not declared anywhere in *int_precision* class and that is the reason you are getting that error. So i have no clue how it can be working under Visual studio.
Anyways, my solution was to add that constructor myself:
in iprecision.h file inside *int_precision* class next to the other constructor declarations add:
int_precision( const float_precision& );
then somewhere in precisioncore.cpp file add:
int_precision::int_precision( const float_precision& s )
{//note that behavior is similar to int(double) cast
//int(9.99) yields 9; and int(-0.9) yields 0;
if(s.exponent()<0)
mNumber = ito_precision_string( int(0), true );
//code taken from int_precision(int) constructor
else
{
mNumber=s.get_mantissa();
if(mNumber[0]=='-'||mNumber[0]=='+')
mNumber.resize(s.exponent()+2);// +1.23456E2 = 123
else
mNumber.resize(s.exponent()+1);// 1.2345E2 = 123
}
}
Note that unlike other constructors this one cannot be inlined as it would create a circular header reference between iprecision.h and fprecision.h headers. That's why the implementation must be in .cpp file.
Hope this helps.

c++ file will not compile with a certain #include while all others do

I have a c++ file that keeps throwing errors about four files up the chain, while every other file using that #include compiles fine.
It keeps giving me this kind of stuff. I am certain the include file is fine as 1. All of the others which use this same file compile fine and 2. I looked at the file, and it is fine.
/usr/include/opal/opal/mediafmt.h:305:9: error: expected identifier before numeric constant
/usr/include/opal/opal/mediafmt.h:305:9: error: expected ‘}’ before numeric constant
/usr/include/opal/opal/mediafmt.h:305:9: error: expected unqualified-id before numeric constant
/usr/include/opal/opal/mediafmt.h:308:14: error: bit-field ‘mode’ with non-integral type
It's odd because it throws the error about 5 files up the chain:
In file included from /usr/include/opal/opal/connection.h:44:0,
from /usr/include/opal/opal/call.h:41,
from /usr/include/opal/opal/manager.h:42,
from /usr/include/opal/opal/endpoint.h:41,
from /usr/include/opal/opal/localep.h:41,
from /home/jonathan/workspace/HHPVideoServer/opal/GstEndPoint.h:12,
from /home/jonathan/workspace/HHPVideoServer/opal/opal-call-manager.h:42,
from ../gui/HHPVideoCodecGui.cc:3:
I checked to make sure I wasn't screwing up any #define s but that was the only idea I had. Is there a good approach someone can offer me on how to track this error down?
Here is the file:
http://www.opalvoip.org/docs/opal-v3_9/da/d60/mediafmt_8h-source.html
Update
Here is the struct with the troublesome line from the preprocessor. I suppose the "0L" is the trouble.
struct H245GenericInfo {
H245GenericInfo() { memset(this, 0, sizeof(*this)); }
unsigned ordinal:16;
enum Modes {
0L,
Collapsing,
NonCollapsing
} mode:3;
enum IntegerTypes {
UnsignedInt,
Unsigned32,
BooleanArray
} integerType:3;
bool excludeTCS:1;
bool excludeOLC:1;
bool excludeReqMode:1;
};
then
#define None 0L
from "/usr/include/X11/X.h" causes 0L to be substituted for None in the Modes enumeration at line 305.
from the X11 lib. The reason the other files were compiling is that they weren't also using Gtk. Does any one have a work around that doesn't involve me rewriting either of the lib files?
solution
I just put the offending #include above everything else. Thanks for the help everyone.
Assuming you're using gcc, run gcc -E your-file.c > tmpfile. This runs just the preprocessor. The output is going to be quite voluminous, but it should help you track down the problem.
What does line 305 look like?
EDIT:
Given your update, yes, the problem is that "None" is being defined as a macro before your mediafmt.h tries to define it as an enumerator. Can you add #undef None after including X.h and before including mediafmt.h? (If so, be sure to comment it.)
EDIT2: Rearranging the #include directives might be a better solution -- but again, be sure to add a comment explaining the reason.
Because you said that this include works from other files, I'm guessing that the order of your #includes may be different in the failing file. Try moving the #include for opal-call-manager.h to the top of the file.
Ideally, you would follow the answers others have posted to track down the root cause, but simply reordering the #includes may be a quick solution and will help narrow down what is conflicting.
Since you found 0L in place of None it's likely you have #define None 0L somewhere in your include chain. Either reorder the includes, like Klox suggested, so this definition happens after mediafmt.h is included, or try and convert, undefine, or fix the offending None definition elsewhere.

VS2008 internal compiler error

I'm consistently running into an internal compiler error while attempting to switch from MSVC6 to MSVC 2008. After much work commenting out different parts of the program, I've traced the error to two lines of code in two different CPP files. Both of these CPP files compile successfully, yet somehow have an effect on whether or not the error manifests in other files.
Both of those lines involve instantianting several complex, nested templates. They also appear to be the only places in the app that use an abstract class as one of the template parameters. That said, I'm far from certain that the issue involves either abstract classes or templates, it's just the most obvious thing I've noticed. I can't even be sure that these lines are significant at all. Here's what they look like, though:
m_phDSAttributes = new SObjDict<RWCString, SIDataSource>(&RWCString::hash);
So we've got SObjDict, a templatized dictionary class, SIDataSource, an abstract interface, and the parameter is a pointer to a static member function of RWCString.
I've been playing around with the code some, and I can occasionally get the error to move from one CPP file to another (for instance, I changed a bunch of template declarations from using class to typename), but I can't find any rhyme or reason to it.
I'm at a loss as to how to debug this issue further. The exact error output by the compiler (with the name of my source file changed) is below. There is no mention of it anywhere on the internet. I'm pretty desperate for any advice on how to proceed. I don't expect someone to say "oh, you just need to do XYZ", but a pointer on how to debug this sort of issue would be greatly appreciated.
1>d:\Dev\webapi.cpp : fatal error C1001: An internal error has occurred in the compiler.
1>(compiler file 'f:\dd\vctools\compiler\utc\src\p2\p2symtab.c', line 5905)
The trick seems to be disabling precompiled headers. I have no idea why that solves the problem, and it's very unfortunate since my build time for the affected project has gone from less than 30 secs to nearly 5 minutes, but at least I can progress forward.
It's a reasonable bet to assume that p2symtab.c is (part of) the symbol table code. This would immediately explain how the upgrade caused it; this code has been rewritten. (Remember the 255 character length warnings of VC6?)
In this case, there is no new entry in the symbol table, so it's likely a lookup in the symbol table failing spectactularly. It would be interesting to see if the context in which th name lookup happens affects the result. For instance, what happens if you change the code to
typedef SObjDict<RWCString, SIDataSource> SObjDict_RWCString_SIDataSource;
m_phDSAttributes = new SObjDict_RWCString_SIDataSource(&RWCString::hash);
This will force another symbol table entry to be created, for SObjDict_RWCString_SIDataSource. This entry is sort of a symbolic link to the template instantiation. The new name can (and must) be looked up on its own.
Start breaking it down into smaller parts. My first guess is the pointer to the static function is going to be the problem. Can you make a dummy non-template class with the same parameter in the constructor? Does it compile if you don't use an abstract class in the template?
Looks like I'm sending you in the wrong direction, the following compiles fine in 2008:
class thing {
public:
static void hash( short sht ) {
}
void hash( long lng ) {
}
};
class thing2 {
public:
thing2( void (short ) ){}
};
int _tmain(int argc, _TCHAR* argv[])
{
thing2* t = new thing2( &thing::hash );
delete t;
return 0;
}
The principle remains though, remove/replace complex elements until you have code that compiles and you'll know what is causing the problem.
fatal error C1001: An internal error has occurred in the compiler.
1>(compiler file 'f:\dd\vctools\compiler\utc\src\p2\p2symtab.c
i also observed the same error when i try to build my vs 2005 code to vs 2008. but it happen till i have not installed Service pack of VS 2008...
have you installed Service pack... i think this will resolved your issue....
This typically happens with template instantiation. Unfortunately it could be caused by many things, but 99% of the time your code is subtly invoking undefined behavior.