Martin
Martin

Reputation: 9379

extern "C" inside namespace

Do you know why the code below fails to compile?

#include <iostream>

namespace C {
    extern "C" {
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> // open()
#include <unistd.h> // read()
    }
}

int main(int argc, char** argv) {
    int fd = C::open("./main.cpp", O_RDONLY);
    C::read(fd, 0, 0);
    return 0;
}

The error from the GCC 4.4 compiler is:

error: ‘read’ is not a member of ‘C’

Upvotes: 1

Views: 903

Answers (1)

Tugrul Ates
Tugrul Ates

Reputation: 9687

You can not inject everything into a namespace under a header. In this case, read is a macro and it is evaluated to something else before namespace resolution rules take effect.

Upvotes: 5

Related Questions