Har
Har

Reputation: 3918

GCC: How to stop gcc from creating random temporary file name for the .o files

I am attempting to compile two .c files using the following gcc command:

gcc -O0 program1.c program2.c -o output.elf

and all is fine until I pass in a linker script and view the map file.

gcc -O0 program1.c program2.c -o output.elf -Xlinker -T memory.ld -Xlinker custom.ld

of which my custom.ld has the following:

  1 SECTIONS
  2 {
  3   .mysection : {
  4     program2.o
  5   }>mymemory
  6 }

and when I view the map file I get the following:

.data           0x00000720        0x4
 .data          0x00000720        0x4 /tmp/ccW6dzJy.o
                0x00000720                GLOBAL_SHARED_INT

where the filename is /tmp/ccW6dzJy.o which means that the wildcard match cannot happen since the linker does not get that information.

So the problem is that the program2.o is not put into the mymemory address because I think the culprit is the filename.

How can I make the gcc preserve the filename so that ld is able to pick up on this?

Upvotes: 1

Views: 1668

Answers (1)

brewbuck
brewbuck

Reputation: 897

When you run the command:

gcc -O0 program1.c program2.c -o output.elf

You are telling the compiler "I don't need the object files, I just want the final linked target."

So if you need the object files, don't do that. Compile the .c files separately, using the -c flag to the compiler. This will produce the individual .o files with the names you expect. Then do the link step.

Upvotes: 6

Related Questions