Having difficulties compiling simple c++ program on the command line with multiple files; maybe a linker error?

  c++, command-line, gcc

Sorry for the simple question. I am attempting to learn more c++ at a fundamental level. I have always used VS in the past, and I am trying to learn the command line and compile, navigate, etc. with it.

I started with "hello world" and was able to compile it with gcc/clang, then run it with the expected results.

I then slightly reworked this and made a new header/cpp file to do the output part of hello world, and then call that from the main function, described below:


#include "MyClass.h"

int main(){
    return 0;


#pragma once

void foo();


#include "MyClass.h"
#include <iostream>

void foo(){
    std::cout << "Hello Worldn";

I then have tried to compile with gcc and clang as follows:

clang -Wall -g main.cpp MyClass.cpp

I have tried the same with GCC, and have also tried various invocations of this, such as using -c:

clang -Wall -g -c main.cpp
clang -Wall -g -c MyClass.cpp

Each and every time, I get an error

λ clang -Wall -g MyClass.cpp main.cpp
main.cpp:13:1: error: use of undeclared identifier 'foo'
1 error generated.

I get this same error whether using gcc or clang.

I also tried from scratch on my laptop, to see if there was some more global issue, but I still get the same problem.

I have also tried on the basic Windows command line as well.

Other areas on StackOverflow demonstrate simple ways of compiling multiple files from the command line, and I have tried as they show, but still get errors.

I also know that "make" is something I need to learn as well, however, I just want to make sure I understand what my make file is doing before I dive into that.

I feel like it must be something trivial that I just cannot figure out.

Source: Windows Questions C++