How to force the compiler to declare functions in C/C++ itself?

online c compiler
function declaration in c
extern c
function declaration c++
function declaration in c example
calling c++ function from c
gcc function attributes
function declaration javascript

The problem: I hate writing headers or declaring my functions if my computer can do it faster. And it is the case. What I tried to do: I tried to declare a function foo after the main function. But the compiler returns an error: error: ‘foo’ was not declared in this scope The code:

#include <iostream>

//no function declaration is allowed, please.
//no other header is allowed, please.

void main() {

void foo() {
    std::cout << "The compiler is smart now!" << std::endl;

I accept to change the compiler if gcc/g++ is not able to compile this c++ code. Any response will be greatly thanked.

This is not possible with freestanding functions as the languages require at least a function prototype to call it. Having said that, in C++ you can use classes to achieve a similar result:

class Main {
    static int main() {
        return 0;
    static void foo() { }

int main() {
    return Main::main();

Whether or not this is a good practice is a different discussion.

Declaring a function - C and C++ Syntax Reference, programming language, all variables must be declared with a specific data type before they can be assigned a value. Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function. Calling a Function. While creating a C function, you give a definition of what the function has to do.

You can use an old (K&R) C compiler. In K&R C, the compiler would implicitly ASSUME any undeclared function that is used returns int and has a variable argument list.

However, that feature has never been supported in C++. It is is also strongly discouraged in modern C (anything more recent than the early 1990s) and disallowed in C standards from 1999. The reason is that such a feature provides lots of opportunities for programmer error (e.g. calling a function with the arguments of incorrect type, and/or assigning the return value to a variable of inappropriate type, both of which often give undefined or otherwise erroneous behaviour).

Function Declaration, If you declare a C++ function to have C linkage, it can be called from a function compiled by the C compiler. A  You can support legacy compilers (i.e. anything without inline) via -Dinline="", although this may waste space if the compiler does not optimize out unused functions.. A GNU C model. Use extern inline in a common header and provide a definition in a .c file somewhere, perhaps using macros to ensure that the same code is used in each case.

I believe you are on the wrong way.

That your compiler did not "know" that you have a function declaration is only half of the story. If you only declare the function than use it and later define it, maybe in a different translation unit, the compiler is not able to inline your code during optimization. Modern compilers can use link time optimization, but that feature must be enabled on command line.

So I would advice you to reorder your code instead of using function declarations from (automated generated) headers. Simply move your definitions in front of the usage in your code.

In C++ it is sometimes useful to write "header only" code and splitting up your software in header and source file is not always the best idea. There are pros and cons for that, but using headers with full implementation offers often better optimization by inlining. As said: Also via LTO possible.

BTW: The task to generate header files from implementation is described already here: Automatically generate C++ file from header?

Must declare function prototype in C?, By declaring a function inline, you can direct GCC to make calls to that function is used (without -fgnu89-inline ), and the third is used when compiling C++. 1. You can force instantiation by using the template with the desired parameter. For example you could define a function using all the required methods: void force_int_instance() { Abstract<int> *a; a->some_method(); a->some_other_method(1, 2, 3); }

This isn't related to GCC, it's universal with C++. The new standard introduces modules which looks promising to fix the 'include hell' issues of the language, bit that's still not quite what you're looking for anyway. There is no way to do what you want, regardless of the compiler, without a declaration beforehand.

What is a Declaration?, 6.30 Declaring Attributes of Functions. In GNU C, you declare certain things about functions called in your program which help the compiler optimize function​  There is no guarantee that functions will be inlined. You cannot force the compiler to inline a particular function, even with the __forceinline keyword. When compiling with /clr, the compiler will not inline a function if there are security attributes applied to the function. The inline keyword is available only in C++.

Mixing C and C++ Code in the Same Program, A function defined in the body of a class declaration is an inline function. You cannot force the compiler to inline a particular function, even  The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs. 3. C function declaration, function call and function definition:

Function declarations -, The C language return statement ends function execution and When a return statement contains an expression in functions that have a void return type, the compiler If a return value isn't required, declare the function to have void to long long to force the // expression to be evaluated as type long long. That way, when the compiler sees lastname in a program, it knows that it’s a string variable. Second, the declarations tell the compiler which type of variable is being used. The compiler knows that integer values fit into the count variable, for example. Third, the compiler knows how much storage space to set aside for the variables.

Inline (Using the GNU Compiler Collection (GCC)), In C++, a function is a group of statements that is given a name, and which can be In the example above, main begins by declaring the variable z of type int , and By passing the arguments by value, the function forces a and b to be copies of Preceding a function declaration with the inline specifier informs the compiler  The default linkage for objects and functions is C++. All C++ compilers also support C linkage, for some compatible C compiler. When you need to access a function compiled with C linkage (for example, a function compiled by the C compiler, or a function written in assembler), declare the function to have C linkage.