18

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.

Jaood
  • 385
  • 1
  • 2
  • 12

8 Answers8

20

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.

xflowXen
  • 396
  • 4
  • 8
3

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.

jskattt797
  • 231
  • 2
  • 10
2

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.

  • Quotes will loc LOCALLY first, and then in the INCLUDE paths (specified in a variety of forms). Braces will NOT look locally but will look in the SAME INCLUDE paths. As a result "your own" header files has a very specific meaning (files that are specified relative to the .cpp/.c) and not "hey I wrote those" – David V. Corbin Aug 23 '22 at 14:56
0

Visual Studio (or rather the compiler) needs to know where to look for the included file. Check out your include path in your VS project.

doron
  • 27,972
  • 12
  • 65
  • 103
0

Edit: This question was just recently updated, this answer is no longer relevant.

For anyone here that still hasn't found a way to fix this, try to modify/redownload Python with the boxes for Download debugging symbols and Download debug binaries Checked.

Read more at a mslearn article and this helpful tutorial site called DelftStack.

0

Note: This answer is good if you use Microsoft Visual Studio IDE.

This error usually occurs when the compiler cannot find the header file.

You can try the following steps:

  1. Right-click on the project name in the Solution Explorer window.
  2. Select Properties.
  3. Under Configuration Properties, click on C/C++.
  4. Under General, add the directory to your header files under the Additional Include Directories section.
  5. Save your changes and try building your project again.
user16217248
  • 3,119
  • 19
  • 19
  • 37
0

Go to project properties and set configuration and platform to ALL and then under C/C++ -> General -> Additional Include Directories add $(SolutionDir)(use this if your solution is stored where your project is stored if not then navigate wherever you have stored your projects header files and hit enter.

Abyss
  • 1
  • 2
-16
#include<iostream.h>
#include<conio.h> 
#include<stdlib.h> 
using namespace std; 

int divide(int num, int den) 
{
   if(den==0) 
   { 
      return -1; 
   } 
   if((num%den)==0) 
   { 
      return 1; 
   } 
   else 
   {    
      return 0; 
   } 
} 

int divide(int a) 
{ 
   int j = a/2, flag = 1, i; 

   for(i=2; (i<=j) && (flag); i++) 
   { 
      if(a%i == 0) 
      { 
         flag = 0; 
      } 
   } 
   return flag; 
} 

void main() 
{ 
   clrscr(); 
   int choice, res, a, b; 

   do 
   { 
      cout<<"1.Check for divisibility\n"; 
      cout<<"2.Check for Prime\n"; 
      cout<<"3.Exit\n"; 
      cout<<"Enter your choice(1-3): "; 
      cin>>choice; cout<<"\n"; 
      switch(choice) 
      { 
         case 1: 
            cout<<"Enter numerator and denominator: "; 
            cin>>a>>b; 
            res = divide(a, b); 
            if(res == -1) 
            { 
               cout<<"Divide by zero error..!!\n"; break; 
            } 
            cout<<((res) ? "It is" : "It is not")<<"\n"; 
            break; 
         case 2: 
            cout<<"Enter the number: "; 
            cin>>a; 
            res = 0; 
            res = divide(a); 
            cout<<((res) ? "It is" : "It is not")<<"\n"; 
            break; 
         case 3: 
            cout<<"Exiting...press any key..."; 
            getch(); 
            exit(1); 
         default:
            cout<<"Wrong choice..!!"; 
      } 
      cout<<"\n"; 
   }while(choice>0 && choice<=3); 
   getch(); 
}
MLeblanc
  • 1,816
  • 12
  • 21