Problem including util.h at compile time

macOS Serra 10.12.6, Xcode 9. The following code compiles with warnings:


#include <stdio.h>

#include <util.h>


FILE * in;

char * line;

size_t len = 0;

size_t lineno = 0;


/* This program segfaults without this declaration. */

/*char *fparseln(FILE *, size_t *, size_t *, const char[3], int);*/


int main() {

in = fopen("testfile.txt", "r");

while((line = fparseln(in, &len, &lineno, NULL, 0)) != NULL) {

printf("%zu:len=%zu %s\n", lineno, len, line);

}

fclose(in);

return 0;

}


$ cat testfile.txt

this is line one

this is line two

this is line three

wow..linefour


$ gcc -I/usr/include -o chk_fparseln chk_fparseln.c

chk_fparseln.c:15:19: warning: implicit declaration of function 'fparseln' is invalid in C99 [-Wimplicit-function-declaration]

while((line = fparseln(in, &len, &lineno, NULL, 0)) != NULL) {

^

chk_fparseln.c:15:17: warning: incompatible integer to pointer conversion assigning to 'char *' from 'int' [-Wint-conversion]

while((line = fparseln(in, &len, &lineno, NULL, 0)) != NULL) {

^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2 warnings generated.


However, fparseln() is defined in util.h. I don't understand why util.h is apparently not being included at compile time. Worse yet, the code segfaults when invoked.


If I uncomment the function declaration in the code above, the code compiles cleanly and runs without errors:


$ ./chk_fparseln

1:len=16 this is line one

2:len=16 this is line two

3:len=18 this is line three

4:len=13 wow..linefour


Any ideas how to include util.h?


-Jason

iMac, macOS Sierra (10.12.6), Xcode 9

Posted on Oct 7, 2017 8:35 AM

Reply
Question marked as Top-ranking reply

Posted on Oct 10, 2017 10:48 AM

Figured out the issue. Wasn't pkgsrc. Something had installed a version of util.h in /usr/local/include. Clang's '--verbose' flag showed all the header file search directories and /usr/local/include was the first. Removing that header file fixed everything.


Pkgsrc is a cross-platform system, though it does have its roots in NetBSD. I've used it under Darwin for several years now and never had an issue with it conflicting with non-pkgsrc tools, which is likely due in part to pkgsrc installing all its files in a separate tree.


I don't disagree with what you're saying about non-native build environments potentially causing havoc, but since I'd already installed pkgsrc on my clean install of Sierra and was able to build my test code and tmux without issues it appeared the problem was not related to pkgsrc, which it wasn't.


I appreciate your insights, and thanks to everyone else who chimed in.


-Jason

9 replies
Question marked as Top-ranking reply

Oct 10, 2017 10:48 AM in response to etresoft

Figured out the issue. Wasn't pkgsrc. Something had installed a version of util.h in /usr/local/include. Clang's '--verbose' flag showed all the header file search directories and /usr/local/include was the first. Removing that header file fixed everything.


Pkgsrc is a cross-platform system, though it does have its roots in NetBSD. I've used it under Darwin for several years now and never had an issue with it conflicting with non-pkgsrc tools, which is likely due in part to pkgsrc installing all its files in a separate tree.


I don't disagree with what you're saying about non-native build environments potentially causing havoc, but since I'd already installed pkgsrc on my clean install of Sierra and was able to build my test code and tmux without issues it appeared the problem was not related to pkgsrc, which it wasn't.


I appreciate your insights, and thanks to everyone else who chimed in.


-Jason

Oct 7, 2017 6:23 PM in response to J. White

J. White wrote:


When you say current command-line tools for each OS do you mean:

El Capitan => Xcode 7.x

High Sierra => Xcode 9


and I should be running Xcode 8.x on Sierra?


Hello Jason,

There are some subtle differences between Xcode and the command-line tools. If you have Xcode installed, you should be able to do "xcode-select --install" and hopefully that will fix the problem. If you don't already have Xcode, you can run something like "git" which will prompt you to install the command line tools.


What is happening is that Xcode doesn't properly setup the build environment for use on the command line. Directories like "/usr/include" don't even exist. This is fine, because most Xcode users are only building for iOS anyway. Mac developers who are correctly worried about distribution issues wouldn't want anything in /usr/include either. All of the includes are in the SDKs tucked deep within the Xcode app. But if you aren't using Xcode to build, then you're in a pickle. That is when you install the command line tools and you can then build code that will run on your machine and likely nowhere else.

Oct 10, 2017 7:47 AM in response to etresoft

I am using the NetBSD pkgsrc system under Darwin, but let's ignore that for a moment and go back to the code snippet I originally posted and the mystery of why util.h doesn't seem to get included at compile time since that it not being built with the pkgsrc framework.


After manually downloading and reinstalling the Sierra command line tools for Xcode 9 I was still getting the implicit declaration of fparseln warnings. I had a Sierra VM that did not exhibit this problem so I knew it wasn't an issue with Sierra or Xcode 9, but with the machine itself.


I first tried reinstalling Sierra over my existing Sierra install -- no difference. Then I wiped my Sierra system, reinstalled from scratch, installed the command line tools and problem solved. Go figure. This machine was upgraded from at least El Capitan and I think Yosemite, so perhaps some cruft leftover.


I have another machine that was upgraded from El Capitan to Sierra which also has the Xcode 9 Sierra command line tools installed that has the same issue compiling that code snippet and getting implicit declaration warnings. I'm hoping that I can figure out how to fix this issue without wiping this system as well. It seems like the dev environment is the same, but I could use some advise on other things to check.


Screenshot below are two systems running 10.12.6 and Xcode 9 dev tools. I would appreciate any input on what I should try next to resolve the issue with the right machine.


User uploaded file


Thanks,

-Jason

Oct 7, 2017 6:06 PM in response to VikingOSX

$ make

gcc -o chk_fparseln chk_fparseln.c -lc

chk_fparseln.c:15:19: warning: implicit declaration of function 'fparseln' is invalid in C99 [-Wimplicit-function-declaration]

while((line = fparseln(in, &len, &lineno, NULL, 0)) != NULL) {

^

chk_fparseln.c:15:17: warning: incompatible integer to pointer conversion assigning to 'char *' from 'int' [-Wint-conversion]

while((line = fparseln(in, &len, &lineno, NULL, 0)) != NULL) {

^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2 warnings generated.


and segfaults when I run.


When you say current command-line tools for each OS do you mean:

El Capitan => Xcode 7.x

High Sierra => Xcode 9


and I should be running Xcode 8.x on Sierra?


Thanks,

-Jason

Oct 7, 2017 6:39 PM in response to etresoft

Thanks for your response. From the beginning I've had the command line tools installed:

$ xcode-select --install

xcode-select: error: command line tools are already installed, use "Software Update" to install updates


$ ll /usr/include/util.h

-r--r--r-- 1 root 4552 Feb 4 2017 /usr/include/util.h


and util.h exists. Is there something else preventing the inclusion of util.h? Even if I specify -I/usr/include to the compiler it's not included. Is that argument not valid or ignored when clang is invoked, even if it's invoked as gcc?


I'm just trying to understand why this fairly simple program won't compile properly. It's the crux of a problem building tmux on Darwin.


Thanks,

-Jason

Oct 7, 2017 8:16 PM in response to J. White

J. White wrote:


It's the crux of a problem building tmux on Darwin.

Hello again Jason,

Your code work fine for me too. I tried tmux and that built and installed without too much effort (aside from installing pkg-config, libtool, autoconf, and automake which I just happen to have handy for my GIS software). Edit: I also had to install libevent as tmux requires that too.


Can I ask what your non-Xcode development environment is like? How did you install those packages I mentioned above? Please don't say anything that rhymes with "sorts", "link", or "shrew". Otherwise, you're on your own.


tmux is interesting. I had never used it before. Thanks.

Oct 10, 2017 9:45 AM in response to J. White

J. White wrote:


I am using the NetBSD pkgsrc system under Darwin, but let's ignore that for a moment

I'm quite sure that is the problem. It always is. Had you used MacPorts, Fink, or Homebrew, you would at least have those communities to ask for support. I haven't heard of anyone using NetBSD pkgsrc before. That puts you into an even smaller category. I don't know what you may have heard elsewhere, but macOS is not BSD.


If you wanted to try to debug the problem, start first with your environment on those two machines. See what is different about them. I have no idea what your package management system has done. Maybe it replaced other parts of your toolchain, like the precompiler.


I built your code (and tmux) on the latest High Sierra beta update with Xcode 9.1. That is a throwaway test machine so I can't hurt anything there. I would never, ever use a package management system on my primary build machine. I don't even use autoconf defaults. When I need to build any of this open source code, I build in a strictly controlled environment that includes a dedicate root tree (platform and OS specific) just for the tools I am building. If you don't need to redistribute and are building only for the Mac, then you don't have to do all of that. But don't waste your time with those package management systems. It seems like a great time saver until you get stuck for 3 days on a problem that should take 3 minutes.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Problem including util.h at compile time

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.