1

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.

StorMy
  • 137
  • 2
  • 3
  • 11
  • Is this relevant? [What are the dangers of using #pragma once?](https://stackoverflow.com/questions/47830610/what-are-the-dangers-of-using-pragma-once) I presume you don't get an error message saying `log.hpp not found`. Note that if the header only contains function declarations, and does not `#include` other headers, you can include it multiple times. – Weather Vane Oct 17 '20 at 17:28
  • I tried using the other method showed in there(aka using ifndef ect.) but the error is still the same. – StorMy Oct 17 '20 at 17:30
  • Where is `log.hpp`? Do you have multiple `log.hpp` files on your system? – Some programmer dude Oct 17 '20 at 17:32
  • It should not matter *what* you call the included file. – Weather Vane Oct 17 '20 at 17:32
  • I do not have any other header files in my system. – StorMy Oct 17 '20 at 17:33
  • It's only compiling `main` there must be some setting you need to change so `log.cpp` is also compiled and linked. – anastaciu Oct 17 '20 at 17:34
  • Have you remembered to *save* the header file? If you look at it from some other program, does it look like you expect? – Some programmer dude Oct 17 '20 at 17:38
  • Yeah, I saved all the 3 files in my program. – StorMy Oct 17 '20 at 17:40
  • By the way, the code you show doesn't match the errors you get. The error for `InitLog` is reported on line 5, which is `int main() {`. And the error for the `Log` call is on line 6 which is the `InitLog` call in the shown code. Please [edit] your question and copy-paste the actual code generating the errors. – Some programmer dude Oct 18 '20 at 00:14
  • Although considering the lines for the error report is shifted up by one from the shown code, it's possible to guess that the `main.cpp` file you're building is missing the `#include "Log.hpp"` line. Double-check that! And not only in your editor, go to the file on the disk and open it in another program. – Some programmer dude Oct 18 '20 at 00:15

2 Answers2

1

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.

arfneto
  • 1,227
  • 1
  • 6
  • 13
1

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
)
micha
  • 1,036
  • 8
  • 12
  • I added the file CMakeLists.txt with your given code inside of it, but when I try to run it, the same thing happens. What am I missing? – StorMy Oct 17 '20 at 20:49
  • Don't use Code Runner but the CMake Tools instead. After installing it, reloading vscode, you should have a run and debug button in the bottom blue bar. Press it, select a kit and run it. Read the Getting Started section in the documentation: https://vector-of-bool.github.io/docs/vscode-cmake-tools/index.html – micha Oct 17 '20 at 21:48