ld: symbol(s) not found for architecture arm64

Hi,all

I'm writing a simple c program on my m1 MacBook with vscode ,but when I complied the project, error occurs:

Undefined symbols for architecture arm64:

  "_main", referenced from:

     implicit entry/start for main executable

ld: symbol(s) not found for architecture arm64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

I installed Xcode and used vscose

and my program is :


#include <cstdio>
int main()
{
    char name[100];printf("please input your name:");
    scanf("%s",name);
    printf("hello,%s",name);
}

Replies

I recommend that you start by getting this running from the command line. That’ll tell you whether the problem is with your command-line tools setup or in how you’re invoking those tools.

Consider this:

% cat test.cpp 
#include <cstdio>
int main()
{
    char name[100];printf("please input your name:");
    scanf("%s",name);
    printf("hello,%s",name);
}
% clang test.cpp
% ./a.out
please input your name:cruel world
hello,cruel
% file a.out
a.out: Mach-O 64-bit executable x86_64

Note If you’re running on Apple silicon, the last command should say arm64 not x86_64.

Does that work for you?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • Hello,

    I am facing a similar error with clang, I am also using the M1 macbook inline-code ld: symbol(s) not found for architecture arm64

    clang: error: linker command failed with exit code 1 (use -v to see invocation) inline-code my code is

    `#include

    #include

    using namespace std;

    int main() {

      cout << "Hello World!!!" << endl;

      int v = 10;

      return 0;

    };`

    I tried what was suggested by eskimo. His code runs fine for me and I get

    'file a.out a.out: Mach-O 64-bit executable arm64'

    but I am unable to run the simple Hello World program. I am a beginner at C++ Could someone please help?

    Thanks in advance

  • Thanks, this worked for me. Good sanity check!

Add a Comment

Hello,

I am facing a similar error with clang, I am also using the M1 macbook


ld: symbol(s) not found for architecture arm64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

my code is

#include <cstdio>

#include <iostream>

using namespace std;



int main() {

  cout << "Hello World!!!" << endl;

  int v = 10;

  return 0;

};

I tried what was suggested by eskimo. His code runs fine for me and I get


file a.out
a.out: Mach-O 64-bit executable arm64

but I am unable to run the simple Hello World program. I am a beginner at C++ Could someone please help?

Thanks in advance

  • For C++, you will need to use "clang++"

  • This worked out for me. Was thinking initially that it's because of the fact that I had the M1 arm64 architecture...

Add a Comment

Hi, I had the same issue, and all I did was save the file I was working with before actually running the code. This worked for me. I'm using an M1 Mac with VS code. Hope this helps.

  • it helps :)) works for me

  • it works for me, too! thanks!

Add a Comment

I generated an Xcode project using CMake and just got this error. Unchecking the 'Shared' box solved the problem. I found it in Product -> Scheme -> Edit schemes -> Build Target

You a genius, that is what worked for me as well. Also learning C++.

I don't understand. none of the above seems to answer the question of what you have to do in order to compile a c++ program or library from command line.

For a start if you search the man pages for information using: man c++ or man clang or man clang++

it brings up the page for "clang" which it says on that page is the compiler for c, c++ and objective-c

here's my command for building object file for a library and the command i am using to try to create and dynamic link library

clang -g -fPIC -mdynamic-no-pic -c slabhra.cpp -o slabhra.o 0>>./tmp 1>>./tmp 2>>./tmp clang -mdynamic-no-pic -target arm64 -dylib -o libarmlann.dylib slabhra.o 0>>./tmp 1>>./tmp 2>>./tmp

Undefined symbols for architecture arm64: "typeinfo for char const*", referenced from: slabhra::getCharStrAtLocation(int) in slabhra.o slabhra::getCharStrAtLocation(int) in slabhra.o "operator new[](unsigned long)", referenced from: slabhra::slabhra() in slabhra.o slabhra::slabhra() in slabhra.o slabhra::slabhra(char*) in slabhra.o slabhra::slabhra(char*) in slabhra.o slabhra::slabhra(char const*) in slabhra.o slabhra::slabhra(char const*) in slabhra.o slabhra::operator=(char*) in slabhra.o ... "___cxa_allocate_exception", referenced from: slabhra::getCharStrAtLocation(int) in slabhra.o slabhra::getCharStrAtLocation(int) in slabhra.o "___cxa_throw", referenced from: slabhra::getCharStrAtLocation(int) in slabhra.o slabhra::getCharStrAtLocation(int) in slabhra.o "___gxx_personality_v0", referenced from: /Volumes/External SSD/valuable/foinse/cód/armlann/slabhra.o "_main", referenced from: <initial-undefines> ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

I am expecting an INCLUDE path update or an install of extra code/object/libraries... i.e. i'm not sure what they mean by symbols.

Thanks in advance for looking at my issue...