简体   繁体   中英

Anyone ever have a case where unistd.h cannot be found on Ubuntu?

I'm having a wicked time trying to find a bug in my system. It's literally driving me mad.

System: Ubuntu 16.04 LTS, gcc&g++ 4.9, 5.3 5.4 available.

Essentially I was trying to compile some code for point cloud registration, I didn't update my machine, I started see that Boost for some reason had disabled threading, generating multiple errors that no threading libraries could be found. I back tracked everything to a section of the boost header looking at GLib definitions, I checked and it seems my compilers cannot see unistd.h in gcc or g++. I checked the files and everything is there, but literally cannot be seen. I tried using the -I flag to get the compilers to look in the directory.

Example c code.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
    int fd1;
    char buf[128];
    fd1 = open(argv[1], O_WRONLY | O_CREAT);
    if (fd1 == -1) {
        perror("File cannot be opened");
        return EXIT_FAILURE;
    }
    scanf("%127s", buf);
    write(fd1, buf, strlen(buf));
    close(fd1);
    return 0;
}

If I try to compile using the command g++ test_unistd.cpp -o main , then I get

/home/user/test_unistd.cpp: In function ‘int main(int, char**)’:
/home/user/test_unistd.cpp:20:32: error: ‘write’ was not declared in this scope
     write(fd1, buf, strlen(buf));
                                ^
/home/user/test_unistd.cpp:22:14: error: ‘close’ was not declared in this scope
     close(fd1);

All the files I can see are there, I can't seem to figure out what the problem is.

Writing up what we figured out in the comments:

There was an empty file at /usr/local/include/unistd.h on OP's system. /usr/local contains unmanaged files (eg things you install manually). Compilers check /usr/local/include first (before /usr/include ), so you can use it to override system functionality. But because /usr/local/include/unistd.h was empty, including it had no effect (except for preventing the real unistd.h from being used).

Solution: Delete /usr/local/include/unistd.h . That way the real header at /usr/include/unistd.h can be found and used again.

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