0

Hi everyone I'm trying to compile a simple class example in C++

I've a MacBook Pro with Apple Silicon M1 Pro with MacOS Monterey 12.6.1.

I use Visual Studio Code as IDE. And I can't compile my code (simple) because this error I throw:

ld: symbol(s) not found for architecture arm64

I'm running the compilation using the "play" button on VSC, and it uses clang++ under the hood (as you can see later in the error). I already tried changing the compiler to gcc and g++ but the problem is still the same.

Follows more details...

main.cpp

#include "TalkToSPI.hpp"

#define sd_cspin 10
#define eth_cspin 12

int main(int argc, char const *argv[])
{
  TalkToSPI t;
}

TalkToSPI.hpp

#ifndef TALKTOSPI_HPP
#define TALKTOSPI_HPP

#include "vector"
#include <string>
#include <iostream>

class TalkToSPI {
  private:
      struct Device {
          uint16_t Id;
          uint8_t CS_Pin; 
      };           

      std::vector<Device> devices;

  public:
      TalkToSPI();
      void addDevice(uint8_t cs_pin, uint16_t device_id = 0);
      bool removeDevice(uint16_t device_id);
      void talkTo(uint16_t device_id);
      void showDevices();
};
#endif

TalkToSPI.cpp

#include "TalkToSPI.hpp"

TalkToSPI::TalkToSPI(){}

void TalkToSPI::addDevice(uint8_t cs_pin, uint16_t device_id) {
   Device d;
   d.Id = device_id;
   d.CS_Pin = cs_pin;

   TalkToSPI::devices.push_back(d);
}

bool TalkToSPI::removeDevice(uint16_t device_id) {
   uint8_t tmp = 0;
   bool res = false;
   for (size_t i = 0; i < devices.size()-1; i++)
   {
       if (devices[i].Id == device_id) {
           tmp = i;
           res = true;
           break;
       }
   }
   
   if (res) devices.erase(devices.begin() + tmp);
   
   return res;
}

void TalkToSPI::talkTo(uint16_t device_id) {

}

void TalkToSPI::showDevices() {
   for (const auto &d : devices) {
       std::cout << d.Id << std::endl;
       std::cout << d.CS_Pin << std::endl << std::endl << std::endl;
   }
}

I looked at the code for a long time (it's a easy easy easy code)... but I still get this error that I don't understand... (P.S. I already tried to remove all spaces in path names... same)

/usr/bin/clang++ -fcolor-diagnostics -fansi-escape-codes -g /Users/antonellobarbone/Documents/GitHub/Inspector/Board_Firmware/TalkToSPI/TalkToSPI/main.cpp -o /Users/antonellobarbone/Documents/GitHub/Inspector/Board_Firmware/TalkToSPI/TalkToSPI/main
Undefined symbols for architecture arm64:
  "TalkToSPI::TalkToSPI()", referenced from:
      _main in main-e2a4e6.o
ld: symbol(s) not found for architecture arm64

If I run the same code on Xcode it works... but my goal is to get working the compiling from Visual Studio Code, not from Xcode or the CLI.

  • 1
    You aren't compiling / linking TalkToSPI.cpp – ChrisMM Dec 03 '22 at 11:13
  • @ChrisMM what do you mean, I'm compiling main.cpp with clang++ the other files are included in it – Antonello Barbone Dec 03 '22 at 11:31
  • 1
    @AntonelloBarbone No they are not. TalkToSPI.hpp is included but TalkToSPI.cpp must be compiled separately. This is basic knowledge on how to build multi file programs. You include header files but each cpp file must be compiled separately. – john Dec 03 '22 at 12:33
  • @john uhm.. I never had this problem before with VSC actually... however I just tried `clang++ -std=c++11 -stdlib=libc++ main.cpp TalkToSPI.cpp -o out` But it still doesn't work.. also I tried compiling TalkToSPI.cpp alone but fails because it does not find main() function.. (because it is a class indeed) – Antonello Barbone Dec 03 '22 at 14:09
  • @AntonelloBarbone Well the command line above looks correct (although I don't know clang++ specifically). Do you get the same error with that command line? – john Dec 03 '22 at 15:30
  • @john the error is different: `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)` – Antonello Barbone Dec 03 '22 at 23:44
  • @AntonelloBarbone Well that new error message makes it seem like it's not linking `main.cpp`. I'm sorry but I don't have an explanation. As suggested you might try using the `-v` option to see the linker command. – john Dec 04 '22 at 06:18
  • I got nothing useful seems... it is very annoying.. I tried also using g++, still errors – Antonello Barbone Dec 04 '22 at 09:58

0 Answers0