Reputation: 379
I'm have an issue with compiling my program with g++ 8.3. I have approx 10 classes in a program. These classes are placed in header files and their full definitions are in .cpp
files. I'm including these classes the same way as in this code:
main.cpp:
#include "CPerson.h"
int main()
{
CPerson person1(10 , "Peter");
CPerson person2(20 , "James");
person1.Print();
person2.Print();
return 0;
}
CPerson.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class CPerson{
protected:
int m_Age;
string m_Name;
public:
CPerson( const int age , const char * name ) :
m_Age(age), m_Name(name){}
void Print(){
cout << "Hello Im " << m_Name << " - " << m_Age << "years old" << endl;
}
};
When I try to compile this C++ program with the following command:
g++ main.cpp CPerson.h
I get this message:
warning: #pragma once in main file
Is here anything I can do about this, or is it just bug in the g++ compiler?
SOLVED:
You need to compile only .cpp files with declarations of methods of each class thats defined in Class.h
Upvotes: 17
Views: 30090
Reputation: 238291
You get the warning because you are compiling a file that contains #pragma once
. #pragma once
is only intended to be used in headers, and there is no need to compile headers; hence the warning. Solution: Don't compile headers.
Upvotes: 33