Home > Back-end >  Why did this happen when I compiled C?
Why did this happen when I compiled C?

Time:12-26

I tried compiling, and I expected it to have no errors (succsessfully). For more information, see https://discord.com/channels/1015095797689360444/1019720925035364422/threads/1056376466612432936 Basically, I create this file:

#include <stdio.h>
int main(){
int num = 100; double pi = 3.1415926536;
printf("Integer is %d \n",num);
printf("Values are %d and %f \n",num,pi);
printf("%} displays } \n",num);
printf("%d displays d \n",num);
printf("Pi is approximately %1.10f \n",pi);
printf("Right-aligned  .3f rounded pi \n",pi);
printf("Left-aligned %-20.3f rounded pi \n",pi);
return 0;
}

as /Users/farhanaazmohammed/Code/vars/c, and when I compile to vars/exe, I get:

ld: warning: ignoring file c, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x23 0x69 0x6E 0x63 0x6C 0x75 0x64 0x65 0x20 0x3C 0x73 0x74 0x64 0x69 0x6F 0x2E )
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Why did this compile with an error only in assembly, and what does it mean?

CodePudding user response:

Programs written in C should be in files whose names have the extension .c. For example, you could name the file display.c or even c.c.

But c is not a valid name. The compiler assumes it is already compiled and tries to pass it directly to the linker, which doesn't work. Since the linker rejects the file, the compiler gives up.

You could use the -x option to define the file's type, but it is far better to choose an appropriate name.

CodePudding user response:

Maybe you're using clang, which is outher compiler

Try to use this code:

g   <file.cpp>
  • Related