Author Topic: error: `sockaddr_in' undeclared (first use in this function)  (Read 19082 times)

Offline eb

  • Multiple posting newcomer
  • *
  • Posts: 46
error: `sockaddr_in' undeclared (first use in this function)
« on: February 03, 2012, 10:38:03 pm »
I have a program (main.c) that includes the header rpc/rpc.h which includes the header netinet/in.h which contains the header cygwin/in.h which contains the struct "sockaddr_in".

So why is it that when I compile main.c I get the error: `sockaddr_in' undeclared (first use in this function) yet when I right click and "Open Include file:" on each of the headers involved it successful opens each successive header properly?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7592
    • My Best Post
Re: error: `sockaddr_in' undeclared (first use in this function)
« Reply #1 on: February 03, 2012, 10:43:16 pm »
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

Under menu "Settings" -> "Compiler and Debugger" -> Global compiler settings -> [the compiler you use] -> "Other Setting" tab

Also check this check-mark "Explicitly currently compiling file's directly to compiler search dirs"

Note: I do not think this will help it is the only item I can think that might help.

Tim S.

« Last Edit: February 03, 2012, 10:48:47 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline eb

  • Multiple posting newcomer
  • *
  • Posts: 46
Re: error: `sockaddr_in' undeclared (first use in this function)
« Reply #2 on: February 04, 2012, 12:00:52 am »
I tried "Explicitly add currently compiling file's directly to compiler search dirs" and "Explicitly add project's top level directory to compiler search dirs" separately and together each time followed by a "Rebuild workspace".  It still can't find it.

Buil log:

-------------- Clean: Release in rap00 ---------------

Cleaned "rap00 - Release"

-------------- Build: Release in rap00 ---------------

gcc-3.exe -Wall  -O2    -IC:/cygwin/usr/include/tirpc -IC:/cygwin/usr/include  -c C:/cygwin/home/eb/rap00/main.c -o obj/Release/main.o
C:/cygwin/home/eb/rap00/main.c: In function `main':
C:/cygwin/home/eb/rap00/main.c:50: error: `sockaddr_in' undeclared (first use in this function)
C:/cygwin/home/eb/rap00/main.c:50: error: (Each undeclared identifier is reported only once
C:/cygwin/home/eb/rap00/main.c:50: error: for each function it appears in.)
C:/cygwin/home/eb/rap00/main.c:50: error: `addr' undeclared (first use in this function)
C:/cygwin/home/eb/rap00/main.c:53: warning: implicit declaration of function `inet_pton'
Process terminated with status 1 (0 minutes, 0 seconds)
4 errors, 1 warnings
 

Offline eb

  • Multiple posting newcomer
  • *
  • Posts: 46
Re: error: `sockaddr_in' undeclared (first use in this function)
« Reply #3 on: February 04, 2012, 12:11:23 am »
Another piece of info:
  When I edit netinet/in.h; when I change "cygwin/in.h" to "cygwin/in1.h" (cygwin/in1.h does not exist) and compile, I get a whole cascade of errors.
So cygwin/in.h IS being successfully accessed but `sockaddr_in' is not being found.


Offline eb

  • Multiple posting newcomer
  • *
  • Posts: 46
Re: error: `sockaddr_in' undeclared (first use in this function)
« Reply #4 on: February 04, 2012, 12:27:46 am »
Sorry folks, looks like it's not a CodeBlocks issue after all.

Just tried and experiment.  Turns out this:

Code
struct sockaddr_in
{
  sa_family_t sin_family; /* Address family */
  in_port_t sin_port; /* Port number */
  struct in_addr sin_addr; /* Internet address */

  /* Pad to size of `struct sockaddr'. */
  unsigned char  __pad[__SOCK_SIZE__ - sizeof(short int)
- sizeof(unsigned short int) - sizeof(struct in_addr)];
};

    sockaddr_in *addr = NULL;

gives you this:
Quote
C:/cygwin/home/eb/rap00/main.c:60: error: `sockaddr_in' undeclared (first use in this function)

PLUS other 'struct' declarations in this file work fine, that is they appear in the CodeBlocks Symbols list.

So there must be a problem with the way the struct is being called out.
Good ol' Cygwin...

Offline eb

  • Multiple posting newcomer
  • *
  • Posts: 46
Re: error: `sockaddr_in' undeclared (first use in this function)
« Reply #5 on: February 04, 2012, 01:06:11 am »
Hold on...
Consider the code:

Code
struct group_filter
{
  uint32_t                gf_interface;
  struct sockaddr_storage gf_group;
  uint32_t                gf_fmode;
  uint32_t                gf_numsrc;
  struct sockaddr_storage gf_slist[1];
};

#define GROUP_FILTER_SIZE(numsrc) (sizeof (struct group_filter) \
  - sizeof (struct sockaddr_storage) \
  + (numsrc) * sizeof (struct sockaddr_storage))

/* Structure describing an Internet (IP) socket address. */
#define __SOCK_SIZE__ 16 /* sizeof(struct sockaddr) */
struct sockaddr_in
{
  sa_family_t sin_family; /* Address family */
  in_port_t sin_port; /* Port number */
  struct in_addr sin_addr; /* Internet address */

  /* Pad to size of `struct sockaddr'. */
  unsigned char  __pad[__SOCK_SIZE__ - sizeof(short int)
- sizeof(unsigned short int) - sizeof(struct in_addr)];
};

CodeBlocks sees group_filter as a symbol but not sockaddr_in.  But if I insert a blank line above "struct sockaddr_in" CodeBlocks sees sockaddr_in and adds it to the 'All local symbols (workspace)' list.

I still get the compile error though. >:(


OH! And "Find declaration of:" finds it now.
« Last Edit: February 04, 2012, 01:08:02 am by eb »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: error: `sockaddr_in' undeclared (first use in this function)
« Reply #6 on: February 04, 2012, 02:55:28 pm »
Hold on...
Consider the code:
[...]
CodeBlocks sees group_filter as a symbol but not sockaddr_in.
Works fine here. However, as you did not mention anything about your configuration (Platform, version) nobody can help you.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline eb

  • Multiple posting newcomer
  • *
  • Posts: 46
Re: error: `sockaddr_in' undeclared (first use in this function)
« Reply #7 on: February 06, 2012, 07:59:19 pm »
Platform Win XP 64
version Code Blocks 10.05