StorMy
StorMy

Reputation: 147

How can I create a c++ header file in Visual Studio Code?

I have a program with 3 files, and when IrRun the program via Code Runner it keeps printing errors.The icon for the "Log.hpp" file is C, not C++. It doesnt matter if I rename it to Log.h or anything, it seems that I cant create a c++ header file in vscode.

The 3 files are:

  1. main.cpp
#include <iostream>
#include "log.hpp"
using namespace std;

int main() {
    InitLog();
    Log("Hello World");
    return 0;
}
  1. log.cpp
#include "log.hpp"
#include <iostream>

void InitLog() {
    Log("Initializing Log");
}

void Log(const char *message) {
    std::cout << message << std::endl;
}
  1. log.hpp
#pragma once

void InitLog();
void Log(const char *message);

The error mesages are:

main.cpp: In function 'int main()':
main.cpp:5:5: error: 'InitLog' was not declared in this scope
     InitLog();
     ^~~~~~~
main.cpp:6:5: error: 'Log' was not declared in this scope
     Log("Hello World");

Need help.

Upvotes: 1

Views: 26690

Answers (4)

Usman Mehmood
Usman Mehmood

Reputation: 194

Here's a way you can properly configure your C++ project using CMake and Ninja.

Project Structure

Your project's file/folder structure should be like this.

project
 |
 |- log
 |   |
 |   |- log.hpp
 |   |- log.cpp
 |   |- CMakeLists.txt
 |
 |- other_module
 |   |
 |   |- other_module.hpp
 |   |- other_module.cpp
 |   |- CMakeLists.txt
 |
 |- main.cpp
 |- CMakeLists.txt

Notice the extra CMakeLists.txt files in every folder. To put it in simple (and potentially incorrect) words, CMake scripts basically tell the compiler where all the relevant files are and which settings to use when compiling them. The CMakeLists.txt alongside main.cpp is the root CMake file.

CMake Scripts

The contents of the root CMakeLists.txt would be like this:

cmake_minimum_required(VERSION 3.19)

get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
project(${PROJECT_NAME} VERSION 0.1 LANGUAGES CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(${PROJECT_NAME} main.cpp)
add_subdirectory(log)
add_subdirectory(other_module)

And the contents of the CMakeLists.txt files in each folder:

get_filename_component(CURRENT_DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
target_sources(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${CURRENT_DIR_NAME}.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE .)

Compilation

Then in the terminal, use CMake and Ninja to compile it.

cmake -G Ninja -B build
ninja -C build

And your executable would be in a folder named build.

project
 |
 |- build
 |   |- some_files ...
 |   |- project.exe <----- Your executable
 |
 |- log
 |   |- some_files ...
 |
 |- other_module
 |   |- some_files ...
 |
 |- some_files ...

The good thing about this approach is that you can add very complex folder structures to your project and it would still be this easy to use.

And you can use it in a similar way on any OS, Windows, MacOS or Linux.

I have made a VSCode extension, "C Toolkit" with which you can create these CMake projects, compile, run and debug them very easily. Although right now it only works with C applications, and not C++, the support for C++ is in the works. :)

Upvotes: 0

Maytea
Maytea

Reputation: 91

It's not a good fix but you can include "log.cpp" itself. It works for me.

Upvotes: 0

micha
micha

Reputation: 1085

Your question is about a specific vscode plugin called Code Runner. It's not really related to vscode. The way how Code Runner is designed will never work for multiple C++ source files. It's not the right tool for the job. Perhaps you'd better off with Microsofts CMake Tools package for vscode. It does require you to create your own CMakeLists.txt file though. In your case you'd need nothing more than:

add_executable(log
    main.cpp
    log.cpp
)

Upvotes: 1

arfneto
arfneto

Reputation: 1765

Are you using Windows? Which compiler? Do you have Microsoft Build Tools installed or even Visual Studio? Are you using gcc or clang?

You must start VS Code from a developer prompt: Open a developer prompt console, navigate to the folder where you code is. Then enter

code .

Visual Studo Code will then open with the environment set up. Then open a terminal inside VSCode using Control-` and try

cl /EHsc main.cpp log.cpp  

And it will create main.exe. You can run it in the terminal and it will not close... Next time you open the project you can just open the folder in VS Code, since it would then already have the json config files created

VS Code is just an IDE. So compilers must be installed and also extensions. And some JSON files and tasks must be set up. It is sometimes simple, sometimes not so simple, I believe. But doing that you will then have a powerful editor and an unbelievably flexible environment, since you can for instance run and debug code in nodejs or C or C++ or anything and even in Linux without leaving the session on your casual Windows machine.

Upvotes: 1

Related Questions