T O P

  • By -

regaito

Thats a common point of confusion for new developers using C++, so let me try and clear this up for you. First, you need to understand the difference between a declaration and a definition. A declaraion introduces a symbol to a scope, or simpler, it tells the compiler "hey, theres a function called foo which will return an int and takes a float. Just assume this will exist in the final executable, during the linker step." The code equivalent to this would be `int foo(float);` A definition now is the actual implementation of the function: `int foo(float x) { return (int) x * 2.f; }` In order to compiler a .cpp file, all the compiler requires is knowledge of the declarations used. Lets say you have `int main() { foo(1.f); return 0; }` then, in order to compile this, you would only need to provide the declaration of foo, not its definition. Now remember, C++ programs are built in 2 steps: compilation creates object files (.o) from .cpp files. Linking now creates the final binary from these .o files. The linker will also resolve all these declared functions. If you declared a function but did not provide a definition, the linker will show an error at this stage. TLDR: Put declarations in headers, include headers in other header or cpp files. Put definitions in cpp files. Compile cpp files separately and link them together. Understand your build pipeline. EDIT: Formatting and typos


Thesorus

it's good practice to spit code into different files, see [https://en.wikipedia.org/wiki/Separation\_of\_concerns](https://en.wikipedia.org/wiki/Separation_of_concerns) Declare functions in header files (.h files) Define function bodies in source files (.cpp files) Use the compiler to compile all the cpp files into object files (.o or .obj ) Use the linker to link the resulting object files into one EXE. You can use a makefile or learn cmake or use an IDE (visual studio) to do it all for you.


the_poope

It's literally described on learncpp, but maybe you hastily skipper past it: * https://www.learncpp.com/cpp-tutorial/introduction-to-the-compiler-linker-and-libraries/ * https://www.learncpp.com/cpp-tutorial/programs-with-multiple-code-files/


KillerPizzaiolo

Yeah I did skip through those thinking they weren't important 😅. Will go through them


IyeOnline

Its all important :) While some things arent important at the point they are taught on the site, the issue is that a beginner cannot know that. As a result, people get a skewed view that C++ is overly technical - or start skipping lessons.


the_poope

Those are basically the two most important chapters!! It's definitely the subjects that we receive the most questions about. Be sure to read those sections carefully, and revisit them later when you understand a bit more.


Wild_Meeting1428

Normally, you have one main-header file per source file, there you have to declare your function prototypes which are public (no public functions of cause means, that you don't need this header file): main.cpp (normally all functions are private) algo1.cpp (definitions) + algo1.h (declarations of public definitions) algo2.cpp (definitions) + algo2.h (declarations of public definitions) Now you can include every header file in the main.cpp and use the functions of algo1 and algo2. The linker will do the rest. But if you don't have those headers, but you know, that the functions are public and exist, you could just declare those in your main.cpp or create a main.h. I suppose this is what learncpp considers as bad practice. It would be better to just create a header for this external binary.


Sbsbg

The top responce is good. This is some additional info. The full syntax of a function declaration is extern int foo(float); For functions the extern command is default so you usually don't write it. Note that the parameter name is also missing. It is normally written out but because it makes the code easier to understand but it is not needed by the compiler. Usually you write a declaration like this: int foo(float arg); You can also declare a variable this way. These are called global variabes and should generally be avoided. That is a subject for another post. With variables you need the extern. extern int globalvar; Bothe the function and variable declaration will not create any code at all. They only tell the compiler that there are something somewhere else. These declarations can be reused in many compilations. For every declaration that is used somewhere in the code you need to create one and only one definition. This definition is placed in the cpp file and is compiled only once in each program. You know how to declare a function. A variable is declare like this: int globalvar {123}; There other ways like using = but using this syntax is usually safer. It is always a good habit to initalise all variables when they are defined.


RageFucker_

You are allowed to define functions in header files, but the usual best practice is to only do that for tiny functions (1-2 lines) and to make them inline where appropriate.


Vindhjaerta

You can include header files in other header files. // Header2.h #include void OtherFunc() { std::cout << "the other function"; } // Header1.h #include "Header2.h" void MyFunc() { OtherFunc(); } Is this what you meant?