Reputation: 123
How do I convince VSCode to compile my C++ program with my own header files? I just can't figure it out.
Minimal example with the following directory structure:
workdir/main.cpp
workdir/test.h
workdir/test.cpp
Content of the main.cpp
:
#include <iostream>
#include "test.h"
int main()
{
std::cout << "Hello world!" << std::endl;
print_stuff();
}
Content of test.h
:
#pragma once
void print_stuff(void);
Content of test.cpp
:
#include <iostream>
void print_stuff()
{
std::cout << "Stuff" << std::endl;
}
From the command line, compiling with g++ -o main.exe main.cpp --std=gnu++17 test.cpp
works. Even VSCode's intellisense correctly finds the functions, but still can't compile.
I've tried modifying the includePath
in the configurations.json
, but so far without any success:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
Upvotes: 2
Views: 2961
Reputation: 123
I found a solution, but the problem was twofold. This post helped: Why is Visual Studio Code ignoring my tasks.json file?
As @drescherjm pointed out, I had to modify my tasks.json
to include all *.cpp files:
"args": [
"-O3",
"${workspaceFolder}/backend/*.cpp",
"-o",
"${workspaceFolder}/backend/main.exe",
"-std=gnu++17",
"-fopenmp"
],
But there seems to be a bug in VSC that changes to the ´task.json´ are not considered right away. Everytime I changed something in the file, I had to restart VSC to take effect.
Upvotes: 1