Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 965303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:58:30+00:00 2026-05-16T01:58:30+00:00

This simple .c file: #include <unistd.h> void test() { char string[40]; gethostname(string,40); } …

  • 0

This simple .c file:

#include <unistd.h>

void test() {
   char string[40];
   gethostname(string,40);
}

… when compiled normally, works fine:

$ cc  -Wall -c -o tmp.o tmp.c
$

… but when compiled in C99 mode, gives a warning:

$ cc -Wall -std=c99 -c -o tmp.o tmp.c 
tmp.c: In function `test':
tmp.c:5: warning: implicit declaration of function `gethostname'
$

The resultant .o file is fine, and linking works. I’d just like to get rid of the warning. I can achieve this in a hacky way, by putting declarations in my own .h file.

What is it about C99 that means the declarations in unistd.h don’t get included?
Can this be overcome, without giving up the niceness of C99?

I see the same problem for other standard libs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:58:31+00:00Added an answer on May 16, 2026 at 1:58 am

    You may need to define some macros in a particluar way to get the prototype for gethostname()

    From man gethostname:

    Feature Test Macro Requirements for
    glibc (see feature_test_macros(7)):

       gethostname(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
       sethostname(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
    

    So:

    #define _BSD_SOURCE
    
    #include <unistd.h>
    
    void test() {
       char string[40];
       gethostname(string,40);
    }
    

    The gory details:

    If you don’t specify the -std-c99 option, then features.h (which is implicitly included by unistd.h) will default to setting _BSD_SOURCE in such a way that the prototype for gethostname() gets included. However, specifying -std=c99 causes the compiler to automatically define __STRICT_ANSI__, which in turn causes features.h to not define _BSD_SOURCE, unless you force it with your own feature macro definition (as above).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ubuntu 11.10 x86_64 I have this this simple test.c file: #include <openssl/ssl.h> int
This is a simple code that open and print the file content. #include <stdio.h>
I have this simple code: #include <iostream> #include <fstream> using namespace std; int main(void)
Hi i'm working on a simple file browser to include in my app this
I have a simple file stlTest2.cpp like this: #include <jni.h> #include <cmath> bool isnan
I write this simple code for saving bitmap to file (*.bmp) from HWND. #include
Consider this simple SVG file : <svg xmlns=http://www.w3.org/2000/svg xmlns:x=http://www.w3.org/1999/xlink viewBox=0 0 353 150> <defs>
I have this simple XML file : <?xml version=1.0 encoding=utf-8 ?> <Artists> <artist artistId=1>
I am having trouble with this simple task: cat file | grep -E ^[0-9]+$
I have this simple code in my user_controller.rb file #listing all users def index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.