Search code examples
gccdriverheader-fileskernel-moduleobject-files

How to properly make a object file from header file (using an I2C board with a debian linux machine)


I have some source files that previously have been compiled for X86 architecture. Now I need to compile them for ARM architecture. When I try to use something like

g++ -c -x c++ foo.h

gcc -c -x c foo.h

it only gives me few instructions. I believe it doesn't link my header file to other included files. I only get "Disassembly of section .comment:". Please note it does prompt me for other files, for example if foo includes foo1.h and foo2.h, if I don't include foo1 and foo2 headers in the same directory the compiler doesn't work.

Another thing which I don't understand is that both gcc and g++ produce the same assembly code, maybe because they only generate the comment section they look the same.

A little bit more details about my problem: I'm using a USB-I2C converter board. The board provides only support for x86/x64 architecture. I managed to access the source file and to get the driver setup. Now I need to test everything together. In order to do so I need to be able to compile a sample code. When I want to do so, it calls on static libraries that need to be in .a extension. I need to create my own library.a file. In order to do so, I have found the source c files (.h header). Now I need to link them together during compilation and make object files, and eventually archive them together in a .a file. Thank you for your help.

Edit (update):

A quick summary of what I have achieved so far: - I was able to find a driver from a github repo. - I was able to make the module - I also compiled a new fresh kernel from a raspbian source code (I'm doing this for a Raspberry PI3):

uname -a
Linux raspberrypi 4.9.35-v7+ #1 SMP Tue Jul 4 22:40:25 BST 2017 armv7l GNU/Linux
  • I was able to load the module properly:

    lsmod Module Size Used by spi_diolan_u2c 3247 0 i2c_diolan_u2c 3373 0 diolan_u2c_core 4268 2 i2c_diolan_u2c,spi_diolan_u2c

    lsusb Bus 001 Device 010: ID a257:2014

    dmesg [ 126.758990] i2c_diolan_u2c: loading out-of-tree module taints kernel. [ 126.759241] i2c_diolan_u2c: Unknown symbol diolan_u2c_transfer (err 0) [ 130.651475] i2c_diolan_u2c: Unknown symbol diolan_u2c_transfer (err 0) [ 154.671532] usbcore: registered new interface driver diolan-u2c-core [ 5915.799739] usb 1-1.2: USB disconnect, device number 4 [10591.295014] usb 1-1.2: new full-speed USB device number 6 using dwc_otg [10591.425984] usb 1-1.2: New USB device found, idVendor=a257, idProduct=2014 [10591.425997] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0 [10591.426005] usb 1-1.2: Product: Diolan DLN1 [10591.426012] usb 1-1.2: Manufacturer: Diolan

What I'm not sure: If the kernel space is properly mapped to physical hardware (if the loaded module can get ahold of my diolan-board)!!! progress: based on my research, I think a hot-plug protocol would take care of that, but not sure! confusion: why the lsusb still only shows the device ID. I know the manufacturer and kernel driver can determine what info to be shown, but only showing ID doesn't seem right to me

What I want to do now: I would like to write a simple c source or python code to interact with my device. Basically I don't know how I can make connection between user space and kernel space. The manufacturer has provided some source code examples, libraries, etc. . However, I have given up on using them because they are for another architecture , based on qt, and I find it near impossible to find replacement for libraries they use in their static libraries ( I figured those libraries by restoring their .a archived file provided for x86)

I just need to know exactly what the next step should be for me to move on towards getting the board working with the PI. Thank you :)


Solution

  • You don't make any object file from a header file. On Linux object files (and executables) are ELF files. Use file(1) (or objdump(1) ...) to check.

    Instead, a header file should be included (by #include preprocessor directive) in some *.cc file (technically a translation unit).

    (You could precompile headers, but this is only useful to improve compilation time, which it does not always, and is an advanced and GCC specific usage; see this)

    You do compile a C++ source file (some *.cc file) into an object file suffixed .o (or a C source file *.c compiled into an object file suffixed .o)

    Read more about the preprocessor, and do spend several days reading about C or C++ (which are different programming languages). Read also more about compiling and linking.

    I recommend to compile your C++ code with g++ -Wall -Wextra -g to get all warnings (with -Wall -Wextra ) and debug information (with -g).

    A minimal compilation command to compile some yourfile.cc in C++ into an object file yourfile.o should probably be

    g++ -c -Wall -Wextra -g yourfile.cc
    

    (you could remove -Wall -Wextra -g but I strongly recommend to keep them)

    You may need to add other arguments to g++. They order matters a lot. Read the chapter about Invoking GCC

    Notice that yourfile.cc contains very likely some (and often several) #include directives (usually near its start)

    You very rarely need the -x c++ option to g++ (or -x c with gcc). I used it only once in my lifetime. In your case it surely is a mistake.

    Very often, you use some build automation tool like GNU make. So you just use make to compile (but you need to write a Makefile - where tabs are significant)

    Notice that some libraries can be header only (but this is not very usual), then you don't build any shared or static ELF libraries from them, but you just include headers in your own C or C++ code.

    addenda

    Regarding your http://dlnware.com/sites/dlnware.com/files/downloads/linux_setup.2.0.0.zip package (which indeed is poorly documented), you should look at the several examples given in the linux_setup/examples/ directory. Such code all have a #include "../common/dln_generic.h" (for instance, 4th line of examples/leds_gui/main.cpp) which itself have other includes. All the examples are Qt applications and provide a *.pro file for qmake (which itself generates a Makefile for make from that .pro file). And passing -x c++ to g++ is rightly not mentioned at all.