Anonymous
#include <iostream> #include "foop.cpp" int main(int argc, char *argv[]) { int x=42; std::cout << x <<std::endl; std::cout << foo(x) << std::endl; return 0; }
Anonymous
On seeing the above thing what I think is this:
Anonymous
#include <iostream> int foo(int a){ return ++a; } int main(int argc, char *argv[]) { int x=42; std::cout << x <<std::endl; std::cout << foo(x) << std::endl; return 0; }
Anonymous
Where is foo(int a) twice in above code?
Anonymous
Can anyone explain where am I getting WRONG?
klimi
i dont get you
I_Interface
Can anyone explain where am I getting WRONG?
what compiler says ? text/code of error ? wrong result ?
klimi
you need to include header, not cpp
klimi
which is said in the StackOverflow
Anonymous
Well, you can look that up on the SO link that I provided.
Anonymous
https://stackoverflow.com/questions/19547091/including-cpp-files
klimi
exactly
klimi
you are telling c ompiler to compile main (with included cpp file) and a foop.cpp
klimi
which in both there is foo function
I_Interface
you need to include header, not cpp
ye, u said it before me)
I_Interface
Klimi is right
klimi
uwU
klimi
klimi should learn cpp
klimi
(i have just read that from SO and it seems logical...)
Anonymous
wait wait.
I_Interface
Klimi is good, catch ur candy)
Anonymous
why twice can you explain again.
klimi
Klimi is good, catch ur candy)
will go to driving scholl meh.. .xD
klimi
so
klimi
include copies the file
Anonymous
yeah I've read that it will be included twice
Anonymous
but why
I_Interface
why twice can you explain again.
coz u including cpp file into ur another cpp file
klimi
so it takes foop.cpp and copies it to main.cpp
Anonymous
so?
Anonymous
yes
klimi
so now there is a foo function in the main.cpp
I_Interface
so that's why 2 foo()
Anonymous
and then?
klimi
one foo function in the main.cpp
klimi
and COMPILER, compiles ALL cpp files
I_Interface
and another in foo.cpp
Anonymous
but main.cpp never included foo()
Anonymous
where?
I_Interface
but main.cpp never included foo()
u included copy of foo.cpp into main.cpp
I_Interface
#include "foo.cpp"
klimi
Anonymous
#include "foo.cpp"
so that means that code of foo.cpp replaces the line #include "foo.cpp". Isn't it?
Anonymous
aah man !!
Anonymous
so
Stanislav
wat ? i want to downvote this shit
klimi
but you are telling the ide to compile all of the code
Anonymous
#include <iostream> int foo(int a){ return ++a; } int main(int argc, char *argv[]) { int x=42; std::cout << x <<std::endl; std::cout << foo(x) << std::endl; return 0; }
Anonymous
Doesn't it look like this at the end?
klimi
it does
Anonymous
so where is foo() twice?
klimi
in the foop.cpp
Anonymous
but I replaced foop.cpp with int foo(int a){ return ++a; }
I_Interface
in links 2 objective files into exe and doesn't know which of 2 foo() must be used - undefined behavior
klimi
i will demonstrate
klimi
i just downloaded the code to local
Anonymous
wait before I say something
klimi
[kurimi@KlimiStation tg]$ g++ main.cpp /usr/bin/ld: /tmp/cckGCRix.o: in function `main': main.cpp:(.text+0x38): undefined reference to `foo(int)' collect2: error: ld returned 1 exit status
Anonymous
I mean to say is that: #include <iostream> #include "foop.cpp" int main(int argc, char *argv[]) { int x=42; std::cout << x <<std::endl; std::cout << foo(x) << std::endl; return 0; }
klimi
[kurimi@KlimiStation tg]$ g++ main.cpp foop.cpp [kurimi@KlimiStation tg]$ ./a bash: ./a: No such file or directory [kurimi@KlimiStation tg]$ ./a.out 42 43
Anonymous
should be converted into
Anonymous
#include <iostream> int foo(int a){ return ++a; } int main(int argc, char *argv[]) { int x=42; std::cout << x <<std::endl; std::cout << foo(x) << std::endl; return 0; }
Anonymous
this.
klimi
oh nvm
klimi
i didnt change to cpp
Anonymous
I just replaced #include "foop.cpp" with the foo() definition. That's it
klimi
[kurimi@KlimiStation tg]$ g++ main.cpp foop.cpp foop.h /usr/bin/ld: /tmp/ccIP2HV7.o: in function `foo(int)': foop.cpp:(.text+0x0): multiple definition of `foo(int)'; /tmp/ccmFdnZw.o:main.cpp:(.text+0x0): first defined here collect2: error: ld returned 1 exit status when i change it to foop.cpp
Anonymous
Why twice?
klimi
look at the build command
Anonymous
Please don't show me any error.