1

I am currently trying to compile a small C program which includes zmq

#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main (void)
{
    //  Socket to talk to clients
    void *context = zmq_ctx_new ();
    void *responder = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (responder, "tcp://*:5555");
    assert (rc == 0);

    while (1) {
        char buffer [10];
        zmq_recv (responder, buffer, 10, 0);
        printf ("Received Hello\n");
        sleep (1);          //  Do some 'work'
        zmq_send (responder, "World", 5, 0);
    }
    return 0;
} 

The system I am using is arm64-darwin and the compiler :

Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0

using the following command :

clang --target=arm64 -I/usr/local/include server.c

which outputs:

/usr/local/include/zmq.h:56:10: fatal error: 'errno.h' file not found

I am currently already 4 hours into this problem and tried already the following:

  • Installing zmq and czmq from source
  • Installing it via vcpkg
  • Compiling with x86_64 and arm64
  • Compiling with gcc11
  • Using it with cmake instead of direct compiler command
  • tried to add stdlibc/c++
  • Looking through countless StackOverflow Posts
ls /usr/local/include
czmq.h                 zdir_patch.h           zmq.h
czmq_library.h         zfile.h                zmq_utils.h
czmq_prelude.h         zframe.h               zmsg.h
node                   zgossip.h              zosc.h
zactor.h               zhash.h                zpoller.h
zargs.h                zhashx.h               zproc.h
zarmour.h              zhttp_client.h         zproxy.h
zauth.h                zhttp_request.h        zrex.h
zbeacon.h              zhttp_response.h       zsock.h
zcert.h                zhttp_server.h         zstr.h
zcertstore.h           zhttp_server_options.h zsys.h
zchunk.h               ziflist.h              ztimerset.h
zclock.h               zlist.h                ztrie.h
zconfig.h              zlistx.h               zuuid.h
zdigest.h              zloop.h
zdir.h                 zmonitor.h

I am pretty sure this is something stupid and easy missing on my site but since I am completely new to c++ and c and come from other languages I can't get my head around it ... I guess I just need the right search term.

If anyone has a solution this is really appreciated but SearchTerms and Resources for getting more understanding of the topic around are also welcomed.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
myfoxit
  • 69
  • 2
  • 8
  • 1
    Have you checked that the standard C header file `errno.h` is actually installed in your system (on POSIX systems it's usually installed in `/usr/include`, don't know about macOS)? How did you install Clang? – Some programmer dude Jan 12 '22 at 12:04
  • 2
    C and C++ are very different languages. – molbdnilo Jan 12 '22 at 12:05
  • On another note, how did you install ZMQ? Why did you install its header files directly in `/usr/local/include` and not a sub-directory? Installing in the root include directory will clutter it up very quickly, and can cause conflicts with future libraries you install. – Some programmer dude Jan 12 '22 at 12:07
  • I could not find this header file which maybe is the reason why it is not working but since this is stdlib could be also that I did not search in the right places since should this not be installed by default ... This was installed via xcode-select --install – myfoxit Jan 12 '22 at 12:07
  • I installed zmq via ```git clone git://github.com/zeromq/libzmq.git cd libzmq ./autogen.sh # do not specify "--with-libsodium" if you prefer to use internal tweetnacl security implementation (recommended for development) ./configure --with-libsodium make check sudo make install sudo ldconfig cd .. ``` – myfoxit Jan 12 '22 at 12:10
  • Which was the official way ...thank you for noting if this is wrong – myfoxit Jan 12 '22 at 12:10
  • Did you install Xcode? What does `xcodebuild -version` show? What does `xcode-select -p` show? If not, what software did you install that includes a compiler and an implementation of the standard C library? – Eric Postpischil Jan 12 '22 at 13:01
  • xcodebuild -version shows 13.2.1 ... xcode-select -p the path /Applications/Xcode.app/Contents/Developer ...I installed gcc via brew and qt at some point which maybe could include a extra compiler – myfoxit Jan 12 '22 at 13:35
  • I probably have to dig deeper ...I update here when I find a solution but thank you @all for the fast suggestions and help – myfoxit Jan 12 '22 at 13:37

1 Answers1

1

On recent versions of macOS, the headers are hidden out of sight rather than being placed in /usr/include as on any civilized system. You may need to set:

export CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include

or:

PartA="/Applications/Xcode.app/Contents/Developer/Platforms/"
PartB="MacOSX.platform/Developer/SDKs/MacOSX.sdk"
export CPATH="$PartA/$PartB/usr/include"

to get the compiler to find the headers — I do that with home-built (now moderately ancient) versions of GCC on my Macs running new versions of macOS. About the only saving grace is that there are no spaces in that directory name.

Setting CPATH may be sufficient. If it isn't, and if the directory it is set to is present, then adding:

… -I "$CPATH" …

to the compiler command line may resolve the problem. I've not needed to do that.

I have this script:

#!/bin/sh
#
# @(#)$Id: gcc-env.sh,v 1.2 2021/11/24 03:02:03 jleffler Exp $
#
# Set GCC environment for macOS Catalina 10.15 (and maybe beyond)

MACOSX_SDK="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
export LIBRARY_PATH="$MACOSX_SDK/usr/lib"
export CPATH="$MACOSX_SDK/usr/include"
export MANPATH="$(clnpath $MANPATH:$MACOSX_SDK/usr/share/man)"

I use . gcc-env to set the environment. My home-built GCC compilers then work correctly.

With that said, the out-of-the-box Xcode installation of /usr/bin/gcc and /usr/bin/clang etc works 'automatically'. However, after installing Xcode, you need to run the GUI (at least briefly). It should offer to install some extra stuff — have it do that extra installation.

When I run the command with the -v option for a particular command of mine, I get the output:

$ clang -v -I/Users/jonathanleffler/inc -g -O3 -std=c11 -pedantic -Wall \
> -Wextra -Werror -Wshadow  -Wmissing-prototypes -Wpointer-arith  -Wold-style-definition \
> -Wcast-qual -Wstrict-prototypes -DHAVE_MEMMEM -DHAVE_STRNDUP -DHAVE_STRNLEN \
> -DHAVE_GETDELIM  -o fl fl.c -L/Users/jonathanleffler/lib/64  -ljl
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name fl.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15 -target-cpu penryn -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 530 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I /Users/jonathanleffler/inc -D HAVE_MEMMEM -D HAVE_STRNDUP -D HAVE_STRNLEN -D HAVE_GETDELIM -I/usr/local/include -O3 -Wall -Wextra -Werror -Wshadow -Wmissing-prototypes -Wpointer-arith -Wold-style-definition -Wcast-qual -Wstrict-prototypes -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-extra-semi-stmt -Wno-quoted-include-in-framework-header -pedantic -std=c11 -fdebug-compilation-dir /Users/jonathanleffler/src/cmd -ferror-limit 19 -fmessage-length 100 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.15.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/vn/wv38v30s4hz3yvbkdtbn049c0000gn/T/fl-e87f7e.o -x c fl.c
clang -cc1 version 11.0.0 (clang-1100.0.33.17) default target x86_64-apple-darwin19.6.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Users/jonathanleffler/inc
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.15.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o fl -L/Users/jonathanleffler/lib/64 /var/folders/vn/wv38v30s4hz3yvbkdtbn049c0000gn/T/fl-e87f7e.o -ljl -L/usr/local/lib -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/lib/darwin/libclang_rt.osx.a
$

See also:

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank you at least a new error ....could be that I completely have to reinstall xcode .../Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/sys/errno.h:72: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/sys/cdefs.h:837:2: error: Unsupported architecture .... Yeah with x64 Linux everything works like a charm – myfoxit Jan 12 '22 at 14:29
  • 1
    Your opinion about “any civilized system” exposes projects to bugs caused by using the default headers when building for other platforms. Ideally, projects and build tools would be crafted to avoid this, and not having default headers is a part of that. The “civilized” thing to do is to take steps to ensure only headers suitable for the target platform are used. – Eric Postpischil Jan 12 '22 at 14:47