'Linker Error with undefined reference to a function [duplicate]
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!
Solution 1:[1]
#include <iostream>
void Logger(const char* std_output)
{
std::cout << std_output << std::endl;
}
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,5) << std::endl;
}
A few things to note:
You defined Logger but did not implement. The definition will not magically be revealed anywhere, unless implemented or coming from another header where it is implemented
You were passing 2 parameters to Multiplied I added a 3rd dummy. You don't utilize int c, so i suggest removing it
Updated Logger to be implemented to just output to std output. You can change it to be as sophisticated as you'd like.
This compiles as is now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Omid CompSCI |