ld: symbol(s) not found for architecture arm64, linking issue

I have just starting coding in my new mac. But whenever i make a program that contains other program files to link. It always shows this arm64 architecture error, i am unable to resolve it. For ex: These are the programs i am trying to create:

//my.cpp

#include "my.h"
#include <iostream>
using namespace std;
void print_foo(){
    cout<<foo<<"\n";
}


void print(int i){
    cout<<i<<"\n";
}
//my.h

#ifndef MY_H
#define MY_H

extern int foo;
void print_foo();
void print(int);

#endif //MY_H
//user.cpp
#include "my.h"



int main(){
    foo = 7;
    print_foo();
    print(99);
}

and this is my Makefile program

output: user.o my.o
	g++ user.o my.o -o output


user.o: user.cpp
	g++ -c user.cpp


my.0: my.o my.h	
	g++ -c my.cpp

clean:
	rm*.o output

This is the output i am getting on running the program:

Answered by DTS Engineer in 736107022

You have declared foo but not defined it. To fix this, add:

int foo;

to the end of my.cpp.

Traditionally, C toolchains would let you get away with this, with the linker consing up a foo for you. I expect there’s some option to enable that as a compatibility mode in Apple’s toolchain but it’s not the right way to solve the problem if you’re writing new code. Your my subsystem clearly owns the foo variable and so it makes sense for it to define it.

Share and Enjoy

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

Accepted Answer

You have declared foo but not defined it. To fix this, add:

int foo;

to the end of my.cpp.

Traditionally, C toolchains would let you get away with this, with the linker consing up a foo for you. I expect there’s some option to enable that as a compatibility mode in Apple’s toolchain but it’s not the right way to solve the problem if you’re writing new code. Your my subsystem clearly owns the foo variable and so it makes sense for it to define it.

Share and Enjoy

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

Thanks, problem is solved. I thought the problem was in the linker, but silly me. It was the code. Thanks a lot.

ld: symbol(s) not found for architecture arm64, linking issue
 
 
Q