1

I need to include a C header file in my C++ project but g++ throws "not declared in this scope" errors. I read that i need to use extern "C" keyword to fix it but it didn't seem to work for me.

Here is a dummy example triggering this error.

main.cpp:

#include <iostream>
extern "C"
{
#include "includedFile.h"
}
int main()
{
    int a = 2;
    int b = 1212;
    std::cout<< "Hello World!\n";

    return 0;
}

includedFile.h

#include <stdint.h>
enum TypeOfEnum {
    ONE,
    TWO,
    THREE,
    FOUR = INT32_MAX,
};

The error thrown is :

$> g++ main.cpp 
In file included from main.cpp:4:0:
includedFile.h:7:9: error: ‘INT32_MAX’ was not declared in this scope
FOUR = INT32_MAX,

I saw on this post that I may need #define __STDC_LIMIT_MACROS without any success.

Any help is welcome!

6
  • why is there a comma after INT32_MAX? Commented Jun 8, 2014 at 3:12
  • yes, but why did you put it there? You don't put a comma after the last thing in an enum, do you? Commented Jun 8, 2014 at 3:14
  • @genisage The comma-after-last-item is actually allowed for enum declarations. Commented Jun 8, 2014 at 3:16
  • A comma at the end of a enum declaration is legal. Commented Jun 8, 2014 at 3:17
  • The code works for me, using g++ 4.8.2. Commented Jun 8, 2014 at 3:19

1 Answer 1

0

<cstdint> is a C++11 feature. Pass -std=c++11 to your compiler.

Sign up to request clarification or add additional context in comments.

2 Comments

Forcing to C++11 doesn't seems right in my case, for portability issues.
@fdeslaur ...then don't use a C++11 feature.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.