简体   繁体   中英

C++ warning: pragma once in main file

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM