Compiling multiple C files in a program

The correct way is as follows: file1.c #include <stdio.h> #include “file2.h” int main(void){ printf(“%s:%s:%d \n”, __FILE__, __FUNCTION__, __LINE__); foo(); return 0; } file2.h void foo(void); file2.c #include <stdio.h> #include “file2.h” void foo(void) { printf(“%s:%s:%d \n”, __FILE__, __func__, __LINE__); return; } output $ $ gcc file1.c file2.c -o file -Wall $ $ ./file file1.c:main:6 file2.c:foo:6 $

.bss vs COMMON: what goes where?

// file a.c // file-scope int a = 0; // goes into BSS after compilation of a.c into object file a.o, a symbol goes into BSS section. // file b.c // file-scope int b; // goes into COMMON section after compilation of b.c into object file b.o, b symbol goes into COMMON section. After linking … Read more

Can I link a plain file into my executable? [duplicate]

You could do this: objcopy –input binary \ –output elf32-i386 \ –binary-architecture i386 my_file.xml myfile.o This produces an object file that you can link into your executable. This file will contain these symbols that you’ll have to declare in your C code to be able to use them 00000550 D _binary_my_file_xml_end 00000550 A _binary_my_file_xml_size 00000000 … Read more

error LNK2005: xxx already defined in MSVCRT.lib(MSVCR100.dll) C:\something\LIBCMT.lib(setlocal.obj)

You are mixing code that was compiled with /MD (use DLL version of CRT) with code that was compiled with /MT (use static CRT library). That cannot work, all source code files must be compiled with the same setting. Given that you use libraries that were pre-compiled with /MD, almost always the correct setting, you … Read more

Linking a C program directly with ld fails with undefined reference to `__libc_csu_fini`

/usr/lib/libc.so is a linker script which tells the linker to pull in the shared library /lib/libc.so.6, and a non-shared portion, /usr/lib/libc_nonshared.a. __libc_csu_init and __libc_csu_fini come from /usr/lib/libc_nonshared.a. They’re not being found because references to symbols in non-shared libraries need to appear before the archive that defines them on the linker line. In your case, /usr/lib/crt1.o … Read more