skip to Main Content

I’m watching this video on the C++ compiler. He mentions how the compiler creates "object files" for the linker. However, when I compile my code using my Mac (Ventura 13.3.1, Apple M2):

clang++ -fcolor-diagnostics -fansi-escape-codes -g src/**/*.cpp -o Main

I do not see these object files which he speaks about.

Why does his compiler create object files and mine does not?

The binary executable runs as expected. Here’s my hello world project.

The YouTuber uses a Microsoft PC to compile his C++ code with visual studio.

I also have visual studio, but running the compiler from visual studio still does not yield object files.

2

Answers


  1. I am new here but I recently had the same questions when I tried to understang how the compiler works.
    I’ve read a lot about the differents steps and here are the four steps with the command to get the differents file :

    gcc -E main.c -o main.i —> the pre-processor step

    gcc -S main.i -o main.s —> compiler

    gcc -c main.s -o main.o or as hello.s -o hello.o —> machine language

    objdump -S main.o —> same but optimized

    gcc main.o -o main.exe or for the linker ld main.o -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -lSystem -o main.exe

    You can do the same commands with clang instead of gcc, I think the result will be the same.

    Sorry if the answer is not well formated.
    I hope I am not totally wrong with my answer and I hope it will help.
    Best

    Login or Signup to reply.
  2. You are using GCC or Clang which are different compared to MSVC.

    You need to specify -c in order to create the object files. Otherwise the compiler will create them as temporary files and pass them to the linker and then eventually they’ll be automatically deleted and you’ll never see them.

    In order to keep them, you need to separate the compilation and linkage steps by specifying the -c option in your compile command. Then enter your link command without specifying that option.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search