I'm new to C++, and I'm trying to better organize my code, by moving methods and variables to header files so they can be better shared amongst .cpp
files. So right now, I'm just trying to learn how best to use header files, but I seem unable to make even the simplest program.
Right now, I have two .cpp
files and one .h
file:
file1.cpp
file2.cpp
header.h
All I want to do is call a function that's defined in file2.cpp
from main()
inside of file1.cpp
.
file1.cpp:
#include <iostream>
#include "header.h"
using namespace std;
void Log(const char* message) {
cout << message << endl;
}
int main() {
InitLog();
Log("Hello World!");
}
file2.cpp:
#include <iostream>
#include "header.h"
using namespace std;
void InitLog()
{
Log("Initializing Log");
}
header.h:
#pragma once
void InitLog();
void Log(const char* message);
I'm running on VScode. Every time I run file1.cpp
, I get this error:
Undefined symbols for architecture x86_64:
"InitLog()", referenced from:
_main in file1-27a581.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
For whatever reason, I can't seem to get file1
to see the function definition in file2
.
Is there something else I should be including in my header file? Do I need to compile things in a certain order? Please help!