Erick Salazar
Erick Salazar

Reputation: 1

Can’t get c++ header files to work on VSC - MACOS

The vscode compiler gave me issues finding my header file, so I created a bash file to run from the integrated terminal since vscode has been finicky anyways. When using my bash file, the compiler cannot find my header file 'Log.hpp' either. I have also followed this Tutorial with no luck. My file hierarchy is my main project folder called 'HEADER',my bash file, and two folders 'HeaderFiles' and 'SourceFiles'.

//Main.cpp
#include <iostream>
#include "Log.cpp"

#include "Log.hpp"

int Main()
{
    InitLog();
    Log("Hello World");
    std::cin.get(); 
}

//Log.cpp
#include <iostream>
#include "Log.hpp"

void InitLog()
{
    log("Initialize log");
}

void Log(const char* message)
    {
    std::cout << message << std::endl;
    }

//Log.hpp
    void InitLog();
    void Log(const char* message);

//build_and_run.sh

#!/bin/bash


SOURCE_DIR="/Users/ericksalazar/Documents/HEADER/SourceFiles"
HEADER_DIR="/Users/ericksalazar/Documents/HEADER/HeaderFiles"
OUTPUT_BINARY="Main"


COMPILER="clang++"
CXXFLAGS="-std=gnu++14 -I\"$HEADER_DIR\" -g -Wall -fcolor-diagnostics -fansi-escape-codes"

SRC_FILES="$SOURCE_DIR/Main.cpp $SOURCE_DIR/Log.cpp"

echo "Compiling..."
$COMPILER $CXXFLAGS $SRC_FILES -o "$SOURCE_DIR/$OUTPUT_BINARY"


if [ $? -eq 0 ]; then
    echo "Build succeeded."
    echo "Running the program..."
   
    "$SOURCE_DIR/$OUTPUT_BINARY"
else
    echo "Build failed."
fi
//properties.json
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}//HeaderFiles/**",
                "${workspaceFolder}/SourceFiles/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

Upvotes: 0

Views: 25

Answers (0)

Related Questions