title: POSIX shared memory and semaphore does't work on Mac OS 10.14.6 and Ubuntu 18.04
body: <p>I wrote a simple c program. Used share memory and semaphore. The clang lsp warned that the the <code>shamt is invalid in c99</code> when I complied the program server.c</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "_shamt", referenced from:
        _main in server-214a3e.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>But I have main function in server.c</p>
  
  <p>Here is the code:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;assert.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/sem.h&gt;
  #include &lt;time.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #define MSG_MAX_SIZE 4096
  
  char *server_memory, *client_memory;
  sem_t *sig_server, *sig_client;
  char message_client[MSG_MAX_SIZE];
  char message_server[MSG_MAX_SIZE];
  
  key_t generate_key(int ID) {
    key_t res;
    if ((res = ftok("/tmp", 'C')) == -1) {
      perror("ftok faild ");
      exit(1);
    }
    return res;
  }
  
  int get_shm_id(key_t key) {
    int res = shmget(key, sizeof(char) * MSG_MAX_SIZE, IPC_CREAT | 0666);
    if (res == -1) {
      perror("server: Failed to open/creat share memory");
      exit(2);
    }
    return res;
  }
  
  int main() {
  
    key_t key_client = generate_key('C');
    key_t key_server = generate_key('S');
  
    int shm_server_id = get_shm_id(key_server);
    int shm_client_id = get_shm_id(key_client);
  
    server_memory = shamt(shm_server_id, (void *)0, 0);
    client_memory = shamt(shm_client_id, (void *)0, 0);
  
    sig_server =
        sem_open("/tmp/signal_server", O_CREAT | 0666, S_IRUSR | S_IWUSR, 1);
    sig_client =
        sem_open("/tmp/signal_client", O_CREAT | 0666, S_IRUSR | S_IWUSR, 1);
  
    while (1) {
      printf("enter message,enter q to quit,enter p to pass input\n");
      fgets(message_server, MSG_MAX_SIZE, stdin);
      if (message_server[0] == 'q') {
        // ：TODO 
        exit(0);
      } else if (message_server[0] != 'p') {
        sem_wait(sig_client);
        strcpy(client_memory, message_server);
        sem_post(sig_client);
      }
      if (sem_trywait(sig_server) == -1) {
        printf("The client is typing\n");
        printf("enter r to reply and or w to wait The other party:");
        char c = getchar();
        if (c == 'w') {
          sem_wait(sig_server);
        } else if (c == 'r') {
          continue;
        } else {
          // :TODO 
        }
      }
      strcpy(message_client, server_memory);
      sem_post(sig_server);
      printf("message recived:%s\n", message_client);
    }
  
    return 0;
  }
  
  </code></pre>
  
  <p>And I also tried to compile the code on Ubuntu 18.04 but there are more errors when compiling.</p>
  
  <pre><code>/tmp/ccrxmeXg.o: In function `main':
  server.c:(.text+0xd5): undefined reference to `shamt'
  server.c:(.text+0xf7): undefined reference to `shamt'
  server.c:(.text+0x11e): undefined reference to `sem_open'
  server.c:(.text+0x143): undefined reference to `sem_open'
  server.c:(.text+0x19c): undefined reference to `sem_wait'
  server.c:(.text+0x1bf): undefined reference to `sem_post'
  server.c:(.text+0x1ce): undefined reference to `sem_trywait'
  server.c:(.text+0x209): undefined reference to `sem_wait'
  server.c:(.text+0x234): undefined reference to `sem_post'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>So how to solve this problem. I want to run this program on my Mac OS but also Ubuntu.</p>
  
  <p>Please help me!</p>