Compiling a Linux program under Mac OS X
NickName:Enrico Ask DateTime:2017-03-08T15:23:32

Compiling a Linux program under Mac OS X

I am trying to use make under Mac OS X (El Capitan) to compile a program which I know to work under Linux. The program makes use of USB libraries. I had to modify the config.mk file for these libraries to be found, but now I end up with errors in the compilation (undeclared identifiers).

Link to source: https://github.com/pali/0xFFFF

It requires usb.h, which seems to be part of usblib-compat. I installed the latter by brew install usblib-compat. But still usb.h couldn't be seen, although I knew where it was: specifically, symbolic link to usb.h and to the library may be found under /usr/local/include and under /usr/local/lib, respectively.

After many trials, I progressed somehow. Namely, the file config.mk is clearly read during the make'ing process, although I have to admit that it is not clear to me how this is done; anyway, I noticed two lines commented:

CPPFLAGS += -I/usr/local/include
LDFLAGS += -L/usr/local/lib -Wl,-R/usr/local/lib

(for the sake of precision, in the original config.mk the local dir was replaced by a pkg dir. I replaced it in these lines.)

I uncommented them and now something happens: the usb.h is found. I think the first of these variable definitions tells the compiler where to look tor header files, and the second tells the linker where to look for libraries - but again it is not completely clear to me.

In any case, I have still problems. Namely, the make'ing process outputs two warnings and an error, and then stops:

usb-device.c:90:57: warning: unused parameter 'udev' [-Wunused-parameter]
static void usb_reattach_kernel_driver(usb_dev_handle * udev, int interface) {
                                                        ^
usb-device.c:90:67: warning: unused parameter 'interface' [-Wunused-parameter]
static void usb_reattach_kernel_driver(usb_dev_handle * udev, int interface) {

usb-device.c:324:13: error: use of undeclared identifier 'RTLD_DEFAULT' if ( dlsym(RTLD_DEFAULT, "libusb_init") )

Seems this program is difficult to port from Linux to Mac, although I think it should be portable. If anyone has any idea about what to do (apart from running a Linux distribution...), it would be much appreciated.

EDIT

dlfcn.h has the following:

#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define RTLD_NOLOAD 0x10
#define RTLD_NODELETE   0x80
#define RTLD_FIRST  0x100   /* Mac OS X 10.5 and later */
/*
 * Special handle arguments for dlsym().
*/
#define RTLD_NEXT       ((void *) -1)   /* Search subsequent objects. */
#define RTLD_DEFAULT    ((void *) -2)   /* Use default search algorithm. */
#define RTLD_SELF       ((void *) -3)   /* Search this and subsequent objects (Mac OS X 10.5 and later) */
#endif /* not POSIX */

Copyright Notice:Content Author:「Enrico」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42665285/compiling-a-linux-program-under-mac-os-x

Answers
Enrico 2017-03-08T22:11:02

Ok, finally I have been successful. I think it be worth publishing my solution - maybe others could find it useful.\n\nSo, the first point is: if I run make in the program's main folder, usb.h is not found. Then, we have to install the corresponding library.\n\nThere are two possibilities for this to be done. The first and more obvious would be to install, through home brew, libusb-1.0 and libusb-compat (the latter provides a compatibility interface for programs that use libusb-0.1, which is the first version of libusb, and is not compatible with libusb-1.0. usb.h is included in libusb-compat):\n\nbrew install libusb\nbrew install libusb-compat\n\n\nHowever, this leads to other problems, as reported in the other answer. I had worked around them, but eventually found out that my program got angry when using libusb-compat (if I understand correctly, interfacing the usb port through two layers of libraries is too slow for a flasher).\n\nSo, the other possibility: installing the actual libusb-0.1. This is not available through home brew. It is however available through ports, with the name of libusb-legacy. So, I had to install ports, install the X-code command line utilities (which required first going to Apples' website to accept their legal things...) and run\n\nsudo port install libusb-legacy\n\n\nOk, now calling make would not do the trick since the compiler is not able to find the library yet. For that, I had to edit the config.mk file which is included in the main directory of the program, uncommenting the last two lines, and editing them somewhat in order to point to the directory where libusb-legacy is stored:\n\nCPPFLAGS += -I/opt/local/include/libusb-legacy -D_DARWIN_C_SOURCE\nLDFLAGS += -L/opt/local/lib/libusb-legacy\n\n\n(the -D_DARWIN_C_SOURCE defines the environmental variable required for other variables to be defined by the libraries. In the Makefile in the src directory, in fact, _POSIX_C_SOURCE is defined.)\n\nDo you think all this did the job? No. In fact at this point I ended up with another error: the linker not being able to find some library called -lusb. I don't know why this syntax, but after some thought I realised that -lusb is somewhat a short for libusb. And the libusb I am using is actually called libusb-legacy... So I went into the Makefile in the src directory, where -lusb is introduced, and changed -lusb to -lusb-compat. Tah-dah! Compiled. A few warnings about non-used variables and a comparison between two different types of integers, but nothing more. And the program runs - after a few trials, I have been able to reflash my bricked phone, which now is alive again! Very happy!!! :)",


More about “Compiling a Linux program under Mac OS X” related questions

Compiling a Linux program under Mac OS X

I am trying to use make under Mac OS X (El Capitan) to compile a program which I know to work under Linux. The program makes use of USB libraries. I had to modify the config.mk file for these libra...

Show Detail

Emulating Linux binaries under Mac OS X

How do I run Linux binaries under Mac OS X? Googling around I found a couple of emulators but none for running Linux binaries on a Mac. There are quite a few posts about running Mac OS X on Linux ...

Show Detail

Cross compile simple standard C program on Linux for Mac

Cross compiling for Mac under Linux is a major PITA. To setup cross compilation you need an Apple developer account, the SDK from XCode etc. I tried it but couldn't get it to work. It seems all the

Show Detail

Compiling a Qt application for Mac OS X on Linux

I'm developing a Qt application and I need to support Linux, Windows and Mac OS X. The only problem however is that I do not own a Mac. Is it possible to compile my application for Mac OS X on Linu...

Show Detail

Cross-compiling in Eclipse CDT with Qt under Mac OSX

Let me list all that I have setup: OS: Mac OSX - 10.6.8 IDE: Eclipse CDT Indigo Framework: Qt 4.7.2 As known, the Qt doesn't have integration to Eclipse on Mac, so I installed everything using ...

Show Detail

How to redirect time command output under Mac OS X

There are several posts discussing the redirection of time command output to a file. For example (time my_program) 2> out { time my_program ; } 2> out They worked under Linux but they did ...

Show Detail

Compiling Source on Mac OS X - Missing ether.h

I'm compiling some C source that includes ether.h, but this file isn't available in my install of Mac OS X. This seems to be a common file included with the Linux OS. Any ideas on how to get this...

Show Detail

Tkinter fonts on Mac OS X

I'm writing a Tkinter/Python program, which I want to run under both Linux and Mac OS X. In Linux I'm very satisfied by the look of it, but under Mac OS X the fonts look really...

Show Detail

How to program a mouse click on Mac OS X and on Linux?

I know how to program a click on Win32 (click on the screen programmatically) using C or Ruby. Does anyone know how to do it on Mac OS X and Linux just as a comparison? thanks.

Show Detail

Compiling yelp under Mac OS X

I am trying to compile both meld and KeePassX again under the Mac OS X environment with the help of Homebrew. I am using an iMac running OS X 10.10. I am currently at the stage of building the

Show Detail