int main(void) giving error - c++

Hello programming world.
I am currently doing my first programming course at university and our instructor said the function
int main (); //without the curly braces// is valid.
I may have misheard him/misinterpreted him, as when I try and run a console with that, it gives an error. But when I do int main() {}; it runs fine.
So:
1. Are the curly braces needed regardless of the content in the body?
How did the function run without the return 0.
Using this, what is the shortest possible int main / void main function possible?
as requested, here is the error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Thank you so much :)

In C++, there are two correct definitions for main:
int main() {
// ...
}
and
int main(int argc, char *argv[]) {
// ...
}
or equivalent. (Other implementation-defined forms are possible, but we can ignore those. And the return type is always int, at least for hosted implementations; void main() is wrong.)
The braces { and } are part of the syntax of a function definition; they're not optional.
You can provide a declaration for main, such as
int main();
but there's no real point in doing so. That's a declaration, not a definition -- and you still need to have a definition somewhere.
A return 0; at the end is not required. This is a special case that applies only to main, not to other functions: if execution reaches the closing }, it does an implicit return 0;.
The shortest legal program would probably be:
int main(){}

Thats the difference between a function definition and declaration (see What is the difference between a definition and a declaration?
Basically int main(); is a prototype telling the compiler that you will have a function called main, which returns an int, but you do not implement it yet.
the int main() {} is the implementation of the function, thus the curly braces, giving it a function body and full implementation.

I'd like to clarify a few things.
int main();
is a function declaration, i.e. it lets other functions / classes know about it.
However, it does not define main, meaning it says nothing about what main actually does.
Since every C++ program must define main, as it is run first, your compiler will definitely give a compile error.
By writing
int main() {}
You are defining main by specifying that main does nothing, so it will run.
Finally, C++ compilers will implicitly add a return 0; statement if you do not return anything, as it is an indicator to the operating system that the program ran successfully.
For more information, see https://stackoverflow.com/a/204483/2512775 on what main should return.

Your error code means that you have not declared the main() function properly. What you should do is add the curly braces to signify the block of code that your application will run in.
Although the compiler will add a return statement if it isn't given one, just add one to make sure.

Related

For a function that takes a const struct, does the compiler not optimize the function body?

I have the following piece of code:
#include <stdio.h>
typedef struct {
bool some_var;
} model_t;
const model_t model = {
true
};
void bla(const model_t *m) {
if (m->some_var) {
printf("Some var is true!\n");
}
else {
printf("Some var is false!\n");
}
}
int main() {
bla(&model);
}
I'd imagine that the compiler has all the information required to eliminate the else clause in the bla() function. The only code path that calls the function comes from main, and it takes in const model_t, so it should be able to figure out that that code path is not being used. However:
With GCC 12.2 we see that the second part is linked in.
If I inline the function this goes away though:
What am I missing here? And is there some way I can make the compiler do some smarter work? This happens in both C and C++ with -O3 and -Os.
The compiler does eliminate the else path in the inlined function in main. You're confusing the global function that is not called anyway and will be discarded by the linker eventually.
If you use the -fwhole-program flag to let the compiler know that no other file is going to be linked, that unused segment is discarded:
[See online]
Additionally, you use static or inline keywords to achieve something similar.
The compiler cannot optimize the else path away as the object file might be linked against any other code. This would be different if the function would be static or you use whole program optimization.
The only code path that calls the function comes from main
GCC can't know that unless you tell it so with -fwhole-program or maybe -flto (link-time optimization). Otherwise it has to assume that some static constructor in another compilation unit could call it. (Including possibly in a shared library, but another .cpp that you link with could do it.) e.g.
// another .cpp
typedef struct { bool some_var; } model_t;
void bla(const model_t *m); // declare the things from the other .cpp
int foo() {
model_t model = {false};
bla(&model);
return 1;
}
int some_global = foo(); // C++ only: non-constant static initializer.
Example on Godbolt with these lines in the same compilation unit as main, showing that it outputs both Some var is false! and then Some var is true!, without having changed the code for main.
ISO C doesn't have easy ways to get init code executed, but GNU C (and GCC specifically) have ways to get code run at startup, not called by main. This works even for shared libraries.
With -fwhole-program, the appropriate optimization would be simply not emitting a definition for it at all, as it's already inlined into the call-site in main. Like with inline (In C++, a promise that any other caller in another compilation unit can see its own definition of the function) or static (private to this compilation unit).
Inside main, it has optimized away the branch after constant propagation. If you ran the program, no branch would actually execute; nothing calls the stand-alone definition of the function.
The stand-alone definition of the function doesn't know that the only possible value for m is &model. If you put that inside the function, then it could optimize like you're expecting.
Only -fPIC would force the compiler to consider the possibility of symbol-interposition so the definition of const model_t model isn't the one that is in effect after (dynamic) linking. But you're compiling code for an executable not a library. (You can disable symbol-interposition for a global variable by giving it "hidden" visibility, __attribute__((visibility("hidden"))), or use -fvisibility=hidden to make that the default).

is there any problem in my code and how do i fix it?

#include <iostream>
void dummyfunction(void)
{
std::cout<< "this is";
}
error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
.How can i fix it??
In a C/C++ program the main() function provides the entry point for program startup. (there are some specialized .ctor functions called before main() that you will rarely, if ever, encounter)
So what you have above declares the function dummyfunction(); but there is no main() function that the linker ld can use as the entry point (the address where program control is passed from the shell to your program to start running)
Your error messages is quite explicit about the lack of main() being the problem. To correct the error you need to define the main() function, where the correct invocations are either int main (void) or int main (int argc, char *argv[]) (you will see char *argv[] written equivalently as char **argv). There is also an env variable, but that isn't important here. If your program takes no arguments on the command line, then int main (void) is proper. With C++ the void can be omitted as it only makes a difference for C.
Including main(), your program will compile with:
#include <iostream>
void dummyfunction(void)
{
std::cout << "this is\n";
}
int main ()
{
dummyfunction();
}
(note: the addition of the '\n' at the end of "this is\n" so the output of your program terminates with a newline as specified by POSIX)
Example Use/Output
I just named to program executable dummyfunction, but you are free to name it anything you like.
$ ./dummyfunction
this is

Define main function in class [duplicate]

This question already has answers here:
in c++ main function is the entry point to program how i can change it to an other function?
(13 answers)
Closed 8 years ago.
I was wondering if its possible to define main() inside a class, something like:
struct runtime_entry_point
{
friend int main()
{
}
};
I have tested that and it doesn't work (Almost in GCC 4.8.2):
g++ -o dist/Release/GNU-Linux-x86/turbo build/Release/GNU-Linux-x86/main.o
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crt1.o:
In function `_start': collect2: error: ld exited with status 1
This sounds to me like a no definition of main() error.
Later, I have written main in the classic way:
struct runtime_entry_point
{
friend int main()
{
}
};
int main(){}
Now the compilation fails because int main() was already defined inside the struct runtime_entry_point! Whats happening here?
Trivially it is not possible to write main as a part of a class/struct. The linker by default searches for a free main method and links against it, makes it the entry point. You may alter this behavior by at the linker level, in which case main must be a static method in class/struct. But this is linker implementation dependent, not-portable and dangerous.
However, in the second situation you mentioned is a result of violation of One Definition Rule. You are defining a name (main()) more than once in a single translation unit.
Аccording to the article on cppreference.com, the following construction:
struct runtime_entry_point
{
friend int main()
{
}
};
Defines a non-member function, and makes it a friend of this class at the same time. Such non-member function is always inline.
Linker couldn't find main() in object file (inline function), and you can't declare another main() in the same translation unit (as it already declared).
You can not define a function twice, it works when you change the definition into a declaration inside the class/struct:
#include <iostream>
struct runtime_entry_point
{
friend int main();
};
int main()
{
std::cout << "Hello, world!" << std::endl;
}
This works just fine with GCC.

How do I obtain a function pointer to the main() method in C++?

I'm working on MS C++ compiler, and have done the next program:
#include <stdio.h>
void main(void)
{
void(*ptr)(void) = &main;
}
I wanted to make a pointer on main() method/function, but has got the next error:
error C2440: 'initializing' : cannot convert from 'int (__cdecl *)(void)' to 'void (__cdecl *)(void)'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
I wonder:
how can I get the pointer of function/method main()
why by default in output is info about int __decl..., but I have exactly wrote void on main() , not int?
Here’s how to obtain a pointer to the main function:
#define DECLARE_UNUSED( name ) (void) name; struct name
int main()
{
int(*ptr)() = &main;
DECLARE_UNUSED( ptr ); // Prevents using `ptr`.
// Don't use `ptr` here. In particular, don't call.
}
Note that
main must have result type int.
calling main (e.g. via that pointer) incurs Undefined Behavior.
It is not necessary to return anything from main; the default return value is 0.
As you can see main is a very special function.
Those rules do not (in general) apply to other functions.
Also note that Visual C++ is wrong in not diagnosing void result type.
Finally, note that writing non-standard void is one character more to type than standard int, i.e., it is just a very, very dumb thing to do. ;-)
PS: Visual C++ is probably mumbling things about int main because it (probably) translates void main to int main internally, and probably it does that to make things link with a non-intelligent linker while actively supporting void main so that e.g. Microsoft’s own non-standard examples in their documentation will compile. That’s my theory #1 anyway, since you ask. But it is, of course, pure guesswork, and it may be that even those who coded that up have no clear idea of why (theory #2).
Well, if you really want to change the entry point of an executable, find the Optional Header by following the steps here, offset 16 bytes and change the 4 bytes. You can find the PE specification here. In order to change the executable file itself while running, you will need some assembly trick, or emit another executable, run a batch and kill the running process.

How can I perform pre-main initialization in C/C++ with avr-gcc?

In order to ensure that some initialization code runs before main (using Arduino/avr-gcc) I have code such as the following:
class Init {
public:
Init() { initialize(); }
};
Init init;
Ideally I'd like to be able to simply write:
initialize();
but this doesn't compile...
Is there a less verbose way to achieve the same effect?
Note: the code is part of an Arduino sketch so the main function is automatically generated and cannot be modified (for example to call initialize before any other code).
Update: ideally the initialization would be performed in the setup function, but in this case there is other code depending on it which occurs before main.
You can use GCC's constructor attribute to ensure that it gets called before main():
void Init(void) __attribute__((constructor));
void Init(void) { /* code */ } // This will always run before main()
You can make the above very slightly shorter by giving "initialize" a return type, and using that to initialize a global variable:
int initialize();
int dummy = initialize();
However, you need to be careful with this, the standard does not guarantee that the above initialization (or the one for your init object) takes place before main is run (3.6.2/3):
It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main.
The only thing that is guaranteed is that the initialization will take place before 'dummy' is ever used.
A more intrusive option (if it's possible) might be to use "-D main=avr_main" in your makefile. You could then add your own main as follows:
// Add a declaration for the main declared by the avr compiler.
int avr_main (int argc, const char * argv[]); // Needs to match exactly
#undef main
int main (int argc, const char * argv[])
{
initialize ();
return avr_main (argc, argv);
}
At least here you're guaranteed that the initialization will take place when you expect.
Here's a somewhat evil method of achieving this:
#include <stdio.h>
static int bar = 0;
int __real_main(int argc, char **argv);
int __wrap_main(int argc, char **argv)
{
bar = 1;
return __real_main(argc, argv);
}
int main(int argc, char **argv)
{
printf("bar %d\n",bar);
return 0;
}
Add the following to the linker flags: --wrap main
eg.
gcc -Xlinker --wrap -Xlinker main a.c
The linker will replace all calls to main with calls to __wrap_main, see the ld man page on --wrap
Your solution in simple and clean. What you can additionally do is to put your code in anonymous namespace. I don't see any need to make it better than that :)
If you are using the Arduino environment, is there any reason you can't place it in the setup method?
Of course, this is after the Arduino-specific hardware setup, so if you have such low-level stuff that it really has to go before main, then you need some constructor magic.
UPDATE:
Ok, if it has to be done before the main I think the only way is to use a constructor like you already do.
You can always make a preprocessor macro of it:
#define RUN_EARLY(code) \
namespace { \
class Init { \
Init() { code; } \
}; \
Init init; \
}
Now this should work:
RUN_EARLY(initialize())
But it's not really making things shorter, just moving the verbose code around.
You can use the ".init*" sections to add C code to be run before main() (and even the C runtime). These sections are linked into the executable at the end and called up at specific time during program initialization. You can get the list here:
http://www.nongnu.org/avr-libc/user-manual/mem_sections.html
.init1 for example is weakly bound to __init(), so if you define __init(), it will be linked and called first thing. However, the stack hasn't been setup, so you have to be careful in what you do (only use register8_t variable, not call any functions).
Use static members of classes. They are initialized before entering to main. The disadvantage is that you can't control the order of the initialization of the static class members.
Here is your example transformed:
class Init {
private:
// Made the constructor private, so to avoid calling it in other situation
// than for the initialization of the static member.
Init() { initialize(); }
private:
static Init INIT;
};
Init Init::INIT;
Sure, you put this in one of your your header files, say preinit.h:
class Init { public: Init() { initialize(); } }; Init init;
and then, in one of your compilation units, put:
void initialize(void) {
// weave your magic here.
}
#include "preinit.h"
I know that's a kludge but I'm not aware of any portable way to do pre-main initialization without using a class constructor executed at file scope.
You should also be careful of including more than one of these initialization functions since I don't believe C++ dictates the order - it could be random.
I'm not sure of this "sketch" of which you speak but would it be possible to transform the main compilation unit with a script before having it passed to the compiler, something like:
awk '{print;if (substr($0,0,11) == "int main (") {print "initialize();"};}'
You can see how this would affect your program because:
echo '#include <stdio.h>
int main (void) {
int x = 1;
return 0;
}' | awk '{
print;
if (substr($0,0,11) == "int main (") {
print " initialize();"
}
}'
generates the following with the initialize() call added:
#include <stdio.h>
int main (void) {
initialize();
int x = 1;
return 0;
}
It may be that you can't post-process the generated file in which case you should ignore that final option, but that's what I'd be looking at first.
There is how I perform pre-main coding.
There are sever init sections executed before main, refers to http://www.nongnu.org/avr-libc/user-manual/mem_sections.html initN sections.
Anyhow, this only works on -O0 optimization for some reason. I still try to find out which option "optimized" my pre-main assembly code away.
static void
__attribute__ ((naked))
__attribute__ ((section (".init8"))) /* run this right before main */
__attribute__ ((unused)) /* Kill the unused function warning */
stack_init(void) {assembly stuff}
Update, it turns out I claimed this function is unused, leading to optimize the routine away. I was intended to kill function unused warning. It is fixed to used used attribute instead.