Thread: Compiling on M1 Mac

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    2

    Unhappy Compiling on M1 Mac

    Hi,

    I've been working in JavaScript and other languages for years and finally decided to take a stab at C and C++. I'm taking a decent online course for experienced JavaScript developers to learn C, and it's been great so far just using the stdlib. But as soon as I start working with my own functions, I start getting compiler errors.

    misc.h

    Code:
    #ifndef MISC_H_
    #define MISC_H_
    void say_hi(void);
    void say_thanks(void);
    void say_bye(void);
    #endif  // MISC_H_
    misc.c

    Code:
    #include <stdio.h>
    #include "./misc.h"
    
    
    void say_hi(void) {
      puts("Hi!");
    }
    
    
    void say_thanks(void) {
      puts("Thanks!");
    }
    
    
    void say_bye(void) {
      puts("Bye!");
    }
    main.c

    Code:
    #include <stdio.h>
    #include "./misc.h"
    
    
    int main(void) {
      say_hi();
      say_thanks();
      say_bye();
    
    
      // printf("Hello, World!\n");
    
    
      return 0;
    }
    If I comment out my functions, and uncomment the printf above, it compiles just fine. If I compile the above, as is, however, I get:

    Code:
    Undefined symbols for architecture arm64:
      "_say_bye", referenced from:
          _main in main-59cdf6.o
      "_say_hi", referenced from:
          _main in main-59cdf6.o
      "_say_thanks", referenced from:
          _main in main-59cdf6.o
    ld: symbol(s) not found for architecture arm64
    Obviously this has something to do with the fact that I'm on an M1 Mac and not on an Intel or AMD machine. But I have no idea what nor how to resolve this issue. I know that the regular gcc compiler doesn't support M1s, but I assumed Apple's compiler would. What am I missing?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You are using the compiler or your IDE wrong.

    If compiling on the command line post the commands you are using.
    If using an IDE post information about your IDE.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2021
    Posts
    2
    Quote Originally Posted by stahta01 View Post
    You are using the compiler or your IDE wrong.

    If compiling on the command line post the commands you are using.
    If using an IDE post information about your IDE.

    Tim S.
    Someone on Reddit just helped me out. I was not aware that I had to specify all the source files at compile time. I thought I just needed the main one.

    So this:

    Code:
    gcc main.c -o main
    Needed to be this:

    Code:
    gcc misc.c main.c -o main

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    I don't use any macs but try:
    Code:
    #include "misc.h"
    
    // Instead of:
    
    #include "./misc.h"
    That is the normal way of including a file in the current directory. The "./" is not needed.

    Also, how are you compiling, from the command line? If so, what is your compile command? Are you sure you are compiling both .c files into the executable?

    EDIT: Sorry! Didn't see the previous responses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-23-2013, 01:44 PM
  2. Compiling C++ to C?
    By silk.odyssey in forum C++ Programming
    Replies: 6
    Last Post: 06-14-2004, 03:38 AM
  3. Compiling Dll's
    By Undeadenemy in forum Game Programming
    Replies: 1
    Last Post: 06-30-2003, 08:11 PM
  4. Compiling
    By djdan in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2002, 07:46 AM
  5. Compiling for DOS
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-18-2002, 05:48 AM

Tags for this Thread