0

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 :)

Nima
  • 13
  • 1
  • 8
  • My current feeling is that you lack too much knowledge to achieve your goals. I would recommend first reading a lot more (about C & C++ programming, about [Linux programming](http://advancedlinuxprogramming.com/), then about Qt programming). You need to spend several weeks in reading and learning first. – Basile Starynkevitch Jul 08 '17 at 05:22
  • If the zip file includes binary libraries and headers but no source files and no instructions on how to build, then the zip file probably doesn't contain the source files necessary to rebuild... – Marc Glisse Jul 08 '17 at 07:02
  • @BasileStarynkevitch, thank you for your response. I have some knowledge in all of those areas. This is something that I have not done before. If I know what I need to do, I should be able to do it. – Nima Jul 10 '17 at 23:31
  • @MarcGlisse, Thank you! I completely agree! The files are indeed poorly documented. Also, you can't find that much on their website. – Nima Jul 10 '17 at 23:44

2 Answers2

1

You don't compile header files, you compile the C files that include them.

All code in a header file should be declarations and type definitions, which give information to the compiler, but don't actually produce any machine code. That's why there's nothing in your object files.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Yes and No! please take a look at following package that they offer: http://dlnware.com/sites/dlnware.com/files/downloads/linux_setup.2.0.0.zip I really believe they compiled their object files from their header files. – Nima Jul 06 '17 at 18:30
  • @Nima I'm absolutely sure they did not. I'm not sure what about the linked package makes you think they did. – sepp2k Jul 06 '17 at 19:22
  • @sep2k I made that potentially wrong conclusion because the content of both files look identical. To my naked eyes, it looks like they compiled the header to an assembly file for x86 architecture. I do know that this doesn't make any sense! – Nima Jul 10 '17 at 23:37
  • @Nima When you say that the contents are identical, do you mean that the object file defines the same symbols that are declared in the header file? Or do you mean more than that (like the actual function bodies being identical - which doesn't make sense since header files shouldn't include function bodies)? It's perfectly common for a header file to have a corresponding .c file that defines exactly those functions that are declared in the header file and then of course the object file compiled from the .c file would also define exactly those symbols. – sepp2k Jul 11 '17 at 09:59
  • File names: dln_i2c_slave.h and dln_i2c_slave.o random content from .o file:00000070 : 70: 53 push %ebx 71: 83 ec 38 sub $0x38,%esp 74: 8b 44 24 40 mov 0x40(%esp),%eax 78: e8 fc ff ff ff call 79 7d: 81 c3 02 00 00 00 add $0x2,%ebx 83: c7 44 24 0c 0c 00 00 movl $0xc,0xc(%esp) 8a: 00 from .h file: \retval DLN_RES_I2C_SLAVE_ADDRESS_NEEDED - I2C slave address must be set before$ */ DLN_RESULT DlnI2cSlaveEnable(HDLN handle, uint8_t port, uint16_t *conflict); – Nima Jul 11 '17 at 17:14
  • @Nima So when you say that the contents are identical, you do mean that the header declared `DInI2cSlaceEnable` and the object file defines it, right? Or do you mean that the `add` and `mov` instructions from the object file somehow are also represented in the header file? Because that does not seem to be the case at all (nor should it be) as far as I can see. From what I see, the header file only contains declarations (as it should) and the object file contains actual function bodies, so it was clearly not compiled from the header, but from a .c file that you don't have. – sepp2k Jul 11 '17 at 17:31
  • Thank you for your prompt reply. I completely agree with you and I exactly know what you are talking about. To me they don't need to have another c source file to define the function. Within the header they have all the information but maybe I'm wrong. More or less we are on the same page – Nima Jul 11 '17 at 17:44
  • @Nima None of the functions inside the header have a body. So there's no information telling the compiler which instructions to generate. All the info in the header is related to types and signatures. All the actual assembly instructions are generated from the code in the .c files (or possibly assembly files). – sepp2k Jul 11 '17 at 18:08
1

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you. I don't see any .c or .cpp file within the linux package they offer. I do believe these headers need to be compiled because they are for communicating with a USB device. – Nima Jul 06 '17 at 18:29
  • Then you need to understand a lot more about C (or about C++) and about the kernel to achieve your goals. Spend several days or weeks in reading stuff. – Basile Starynkevitch Jul 06 '17 at 18:30
  • Something like this gcc -c -x c foo.h gives me what I want, the only problem is that it is incomplete. It doesn't seem to link properly – Nima Jul 06 '17 at 18:36
  • This command `gcc -c -x c din_i2c_slave.h` is very likely to be very wrong – Basile Starynkevitch Jul 06 '17 at 18:38
  • I just double checked .h file and .o file on an X86 machine. I can tell they are the same. I'm not sure why and how, but I can definitely say the .o file comes from the .h file – Nima Jul 06 '17 at 18:43
  • This is a mistake in your build or your setup. BTW, try `file *.o` and `file *.h` – Basile Starynkevitch Jul 06 '17 at 18:44
  • I gave up on compiling those files. But I still have a strong feeling that both the headers and object files are identical... – Nima Jul 10 '17 at 23:35