Jump to content

C++ Project in VS Code with header files doesn't compile

I have a simple 3 file C++ project that I want to be able to compile and build in VS Code.

A main file(main.cpp), a header file(time.h) and its .cpp equivalent(time.cpp)

Problem is, VS Code doesn't recognize that these files are related. All functions of the class "time" are marked as errors and it fails to compile.

 

I have tested the 3 files in Dev C++ and they work, so the problem is with VS Code.

I have installed MSYS2 and GCC compiler and added the msbuild file path the user environment variables path.

 

In VS Code I did create a tasks.json, launch.json and c_cpp_properties.json file and tried messing with them to make it work but it still doesn't work.

When I run the build task is says "Build finished with error(s)." and the main file on it's own doesn't compile due to the errors.

 

What am I missing?

Link to post
Share on other sites

The most common problem is needing to setup an include path in the compiler/project settings, to tell it where to find #include'd files. It's probably failing to find the function/class prototypes in the header file, because it doesn't know where to find the header file.

 

EDIT: It might be preferring the standard library time.h over your time.h. It might be enough to just try a different filename.

Link to post
Share on other sites

On 3/24/2022 at 10:19 AM, Mojo-Jojo said:

It might be preferring the standard library time.h over your time.h. It might be enough to just try a different filename.

This was it. After creating a new project vs code worked fine. Dev C++ happened to use an older version of the compiler and thus worked fine, which only added to my confusion.

 

In my header files I always start with:

#ifndef TIME
#define TIME

class time { ... };
#endif

which I thought was supposed to cover cases such as this but apparently not? I'm not entirely sure what that does now.

Link to post
Share on other sites

20 hours ago, Adonis4000 said:

This was it. After creating a new project vs code worked fine. Dev C++ happened to use an older version of the compiler and thus worked fine, which only added to my confusion.

 

In my header files I always start with:

#ifndef TIME
#define TIME

class time { ... };
#endif

which I thought was supposed to cover cases such as this but apparently not? I'm not entirely sure what that does now.

It means it should only be included/defined once, otherwise you end up redefining it every time the header is included causing a self conflict.

 

When you have conflicting file names you should include them using a relative path rather than a globally defined one.

You can setup your environment to prefer local includes over global, which is possibly what your Dev C++ Environment was doing. You however shouldn't as it causes confusion down the line.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×