Aasif
linker links ur obj files into executable file
I'm really sorry, but this is what Google says too, and I'm not sure I follow. What I don't understand is simple : let's say that you have a source file(A.cpp), you include certain files of interest in it and then simply compile that file(A.cpp) to get your executable. why do we need anything else? I've thought about it and I'm lost.
Foxner
i want an output like e em eme emen emenp emenpl emenpla emenplac
I'd generate the entire string first, than output substrings
Francisco
And hence, why do we need makefiles too?
Cause you normally want to modularize the code, and makefiles help in building and linking all those files
Aasif
ur cpp files translate into machine language (machine units), files only with bites, then into obj files, then linto linker
Let's say we have a file(A.cpp) which has only the content #include<iostream> int main(){cout<<"hi";} Could you please walk me through it's compilation process ? Thanks.
Francisco
Let's say we have a file(A.cpp) which has only the content #include<iostream> int main(){cout<<"hi";} Could you please walk me through it's compilation process ? Thanks.
iostream is a file in the computer, whose path the compiler already knows. The compiler transforms the .cpp (source) in a .o (object), then the linker transforms the .o file in the final binary
Francisco
Is iostream a .cpp file?
.cpp is just an extension. iostream is a header, which contains declarations (and also definitions)
I_Interface
But I thought the linker linked files.
linker links ur object files only into 1 exe file
Aasif
linker links ur object files only into 1 exe file
Can we say it as a simple conversion only? That it converts object file to an executable?
Aasif
linker links ur object files only into 1 exe file
So it links several object files into a single executable?
I_Interface
Francisco
But I thought the linker linked files.
When you include a header file, you just get the API, but the actual implementation of that API may be in another already compiled file, which is linked later
Francisco
If you don't link the objects, the linker might complain that some definitions are missing, even though the compiler did its job without errors
Francisco
So including is not a simple recursive copying of the file contents to the source file, right ?
Let's think differently. If you declare a function with no body, the compiler is fine with it, as long as you provide an object file or a library with the implementation to the linker
Roy
So including is not a simple recursive copying of the file contents to the source file, right ?
This can be recursive when one header file is included in several .cpp files or in other header files. To prevent this, you can use #pragma once or use macros
Aasif
Or what is a library?
Francisco
Is library an object file?
Not exactly, but close enough
Aasif
Not exactly, but close enough
So a library is different from a cop file, an object file and an excutable - it's entirely different.
Francisco
But that's another topic
Aasif
*cpp file.
Francisco
Libraries are essentially object files that are linked in a clearer way
Aasif
Aasif
And it shouldn't be thought in those terms.
Francisco
What people normally do is separate declarations (.h, header) from definitions/implementations (.cpp, source)
Francisco
So you compile the source only once and not everytime you compile the main file
Francisco
Where the actual implementations of your functions are
Aasif
But theoretically it is possible to write a single file cpp file for all the projects, but it would be highly unadvised because of modularity issues, am I correct?
Aasif
Yes.
Roy
Yes.
https://www.hackerearth.com/ru/practice/notes/what-happens-when-a-c-program-runs/ This will help a little understanding of the compilation and assembly process.
Sachin
#include<bits/stdc++.h> using namespace std; int main(){ vector<pair<string,pair<int,int>>> v; v.push_back(make_pair("hello",make_pair(3,5))); v.push_back(make_pair("world",make_pair(5,6))); for(auto element:v){ cout<<element.first<<endl; cout<<element.second.first<<" "; cout<<element.second.second<<endl; } sort(v.begin(),v.end(),compare) return 0; }
Sachin
Why not using tuple<string, int, int> instead? That's not a very friendly snippet of code
Actually it is given and mandatory ...can you tell me how to do this?
Francisco
Actually it is given and mandatory ...can you tell me how to do this?
Look for an example in cppreference on how to write a custom comparator, I'm pretty sure there's plenty of examples
Aasif
Including a file literally means copying the content of one file in another
I'm sorry, but another question: when including means copying the contents of a file into main file, then, let's say a file(A.cpp) includes iostream, and iostream includes ios, does it mean that the code of ios too is copied to the main file?
Aasif
Nobody wants a file with 50.000 lines of code
So eventually what happens is the main file becomes very lengthy.
Sachin
Yes
Can you tell me how to write functor and comatator ? I m facing problems to write them
Francisco
There's plenty or examples on the Internet. I don't have a laptop in front of me right now, so I don't feel like writing an example
Francisco
Search on google "How to write custom comparator C++"
Francisco
It should give you what you need
Aasif
So eventually what happens is the main file becomes very lengthy.
And when the main file becomes too lengthy, doesn't it mean that the code of iostream ends up being compiled again? Because now, it is included in the main file itself?
Francisco
Also iostream is template based, so you have no other options
Aasif
Also iostream is template based, so you have no other options
So what I understood is this: by including a file, we literally copy its contents to the main file, but its definitions are in some other files which have already been compiled earlier, and those object files are linked to the object file of the main file to create a final executable. Am I correct?
Francisco
You got it well enough 👍
Sai Meghana
Sai Meghana
#include <iostream> #include <cmath> using namespace std; int check(int n); int check(int n) { if (n=0) return 0; if (n=1) return 1; if (n=2) return 2; return floor(check(n/2))+floor(check(n/3))+floor(check(n/4)); } int main() { int n; int gift; cin>>n; while(n!='\0') { gift=check(n); cout<<gift<<endl; cin>>n; } return 0; }
Sai Meghana
I am trying to learn Dynamic Programming and cant quite figure out where I'm going wrong...
Sai Meghana
pastebin?
Sai Meghana
equality sign
omg how did i not see that
Sai Meghana
i changed that.. still got some problem.. is that long return statement with floor permitted?
Sai Meghana
it hasn't passed in any case XD
Sai Meghana
Aasif
I guess just doing gift=check(n); cout<<gift would do it.
Sai Meghana
because it has to take multiple inputs and print the output for each one
Sai Meghana
12 n 2 are two different inputs
Sai Meghana
and there can be any number of inputs
Sachin
and there can be any number of inputs
Arw you doing competitive programming
Sai Meghana
yeah, they had given this as a beginner stage..