I am new to Stack OF and C++. And I am following a tutorial by The Cherno wherein at 7:00 he is explaining how the linker would not try search for a function "Log" if the it is present in an unused static function. Which makes sense!
However upon me trying it. I don't get the same result and I do get a linker error.
Here is my code:
main.cpp
#include <iostream>
void Logger(const char*);
static int Multiplied(int a, int b, int c){
Logger("Hello");
return a * b;
}
int main(){
//Logger("Hello World from Log function \n");
//std::cout << Multiplied(3,4) << std::endl;
}
my source directory only has main.cpp
ls -lrth src
total 4,0K
-rw-rw-r-- 1 ubuntu18 ubuntu18 232 Feb 22 04:19 Main.cpp
The output directory has following file:
ubuntu18@ubuntu18:~/dev/HelloWorld$ ls -lrth CMakeFiles/HelloWorld.dir/src/
total 48K
-rw-rw-r-- 1 ubuntu18 ubuntu18 313 Feb 22 01:15 Main.cpp.i
-rw-rw-r-- 1 ubuntu18 ubuntu18 412 Feb 22 01:20 Math.cpp.i
-rw-rw-r-- 1 ubuntu18 ubuntu18 37K Feb 22 04:22 Main.cpp.o
Error:
ubuntu18@ubuntu18:~/dev/HelloWorld$ make all
/usr/bin/cmake -H/home/ubuntu18/dev/HelloWorld -B/home/ubuntu18/dev/HelloWorld --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/ubuntu18/dev/HelloWorld/CMakeFiles /home/ubuntu18/dev/HelloWorld/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/ubuntu18/dev/HelloWorld'
make -f CMakeFiles/HelloWorld.dir/build.make CMakeFiles/HelloWorld.dir/depend
make[2]: Entering directory '/home/ubuntu18/dev/HelloWorld'
cd /home/ubuntu18/dev/HelloWorld && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ubuntu18/dev/HelloWorld /home/ubuntu18/dev/HelloWorld /home/ubuntu18/dev/HelloWorld /home/ubuntu18/dev/HelloWorld /home/ubuntu18/dev/HelloWorld/CMakeFiles/HelloWorld.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/ubuntu18/dev/HelloWorld'
make -f CMakeFiles/HelloWorld.dir/build.make CMakeFiles/HelloWorld.dir/build
make[2]: Entering directory '/home/ubuntu18/dev/HelloWorld'
[ 50%] Linking CXX executable HelloWorld
/usr/bin/cmake -E cmake_link_script CMakeFiles/HelloWorld.dir/link.txt --verbose=1
/usr/bin/c++ -Wall -g CMakeFiles/HelloWorld.dir/src/Main.cpp.o -o HelloWorld
CMakeFiles/HelloWorld.dir/src/Main.cpp.o: In function `Multiplied(int, int, int)':
/home/ubuntu18/dev/HelloWorld/src/Main.cpp:5: undefined reference to `Logger(char const*)'
collect2: error: ld returned 1 exit status
CMakeFiles/HelloWorld.dir/build.make:97: recipe for target 'HelloWorld' failed
make[2]: *** [HelloWorld] Error 1
make[2]: Leaving directory '/home/ubuntu18/dev/HelloWorld'
CMakeFiles/Makefile2:70: recipe for target 'CMakeFiles/HelloWorld.dir/all' failed
make[1]: *** [CMakeFiles/HelloWorld.dir/all] Error 2
make[1]: Leaving directory '/home/ubuntu18/dev/HelloWorld'
Makefile:86: recipe for target 'all' failed
make: *** [all] Error 2
I would love to know the explanation. Why is it so?
Edit: Updated video timestamp and, Here is the compiler information in my CMake
set(CMAKE_C_COMPILER_ID "GNU")
set(CMAKE_C_COMPILER_VERSION "7.5.0")
NOTE: I would appreciate if you could watch the video tagged in the first line from 7:00 to understand the problem better. Thanks!