C++ Fundamentals / Organazing functions in files issue

Hello folks,

I am running into a bit of an issue with the tutorial.

This is the error I am getting.

c++ /home/sitrixs/Clion/ClionProjects/HelloWorld/main.cpp -o main
/usr/bin/ld: /tmp/ccmfqRb9.o: in function main': main.cpp:(.text+0x30): undefined reference to greet(std::__cxx11::basic_string<char, std::char_traits, std::allocator >)’
collect2: error: ld returned 1 exit status

Here are is the code of my files.

Cmake:

cmake_minimum_required(VERSION 4.0)
project(HelloWorld)

set(CMAKE_CXX_STANDARD 20)

add_executable(HelloWorld main.cpp utils/greet.cpp)

main.cpp:

#include <iostream>
#include "utils/greet.hpp"

using namespace std;


int main() {
    greet("Guy");

    return 0;
}

greet.hpp:

#ifndef UTILS_GREET
#define UTILS_GREET
#include <string>

void greet(std::string name);

#endif

greet.cpp:

#include <iostream>

using namespace std;

void greet(string name) {
    cout << "Hello, " << name;
}

The only way I managed to get this working, is adding #include greet.cpp into the main.cpp or to the greet.hpp , this allows the code to compile, if i try to follow the step by step in the video, i get an error mentioned above.

I am running this in Clion, on Bazzite (Linux Distro) so not sure if it has something to do with the error.

Looking forward for your help.

Thanks.

It seems something is wrong in your makefile. greet.cpp get first compiled in an object file and then the linker (ld) binds everything together to an executable. So in case ld starts crying. The .o -file for linking does not exist, - or compile the .o objectfile is not done properly.

All these things are define in the makefile. So check this first.

Edit the CMakeList.txt. You find the line with add_executable.
Add the filename greet.cpp to the list. greet.cpp is in the utils directory in the video

add_executable(HelloWorld main.cpp utils/greet.cpp)

This should fix the problem. The reason is explained in my first comment. So this seems to be ok in your example. Then look, if you edited the right CMakeList.txt or execute the build command in the right directory.