Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ cannot open source file

In C++ with Visual studio 2017,

I copied some header files into my project folder, then added them under the "solution explorer" in c++. Now when I write

#include "name.h"

it prints an error under include, and says "cannot open source file".

Why, and what can I potentially do to fix it?

I only just downloaded VS and am learning c++ for the first time.

like image 313
Jaood Avatar asked Mar 08 '17 19:03

Jaood


People also ask

How do I add a header file in Visual Studio?

Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.


3 Answers

If you're using Visual studio, right click on the project and then on Properties, Under Configuration Properties click on C\C++ and then add the directory to your header files under the Additional Include Directories section.

like image 150
xflowXen Avatar answered Nov 18 '22 06:11

xflowXen


There is more information here on how to deal with this problem: Where does Visual Studio look for C++ header files?

For me, I followed xflowXen's answer and then at "Include Directories" typed in the specific pathname where my header file was located followed by a semicolon, something like: C:\Users\name\source\repos\p2-A\p2-A; then applied the changes and the issue went away.

like image 41
jskattt797 Avatar answered Nov 18 '22 06:11

jskattt797


For anyone still scratching their heads, you're not "supposed to" #include your own headerfiles with triangle quotes (<>), You're supposed to use "Quotation marks". It is a common mistake.

like image 33
Falling Forward Avatar answered Nov 18 '22 06:11

Falling Forward