0

I am new to C++. I have a problem with including the header. I'm trying to compile my a.cpp file and get this error. I'm using Sublime Text as a text editor. Can anyone help me with this problem? Thank you in advance!

Undefined symbols for architecture arm64:
  "print()", referenced from:
      _main in a-3ccfb2.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here are my file:

/* Filename: a.cpp */
#include <iostream>
#include "b.h"
using namespace std;

int main() {
    print();
    return 0;
}
/* Filename: b.h */
#ifndef _b_h 
#define _b_h

void print();

#endif
/* Filename: b.cpp */
#include <iostream>
#include "b.h"
using namespace std;

void print() {
    cout << "Hello, world!" << endl;
}

I compile my cpp with: (this works fine except when I try to include my header file - example: #include "b.h")

g++ -std=c++17 -Wall a.cpp  -o a

g++ --version

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
tobias
  • 1
  • 1
  • 1
    Are you sure you have followed the directions to tell Sublime how to find and use your compiler? (If you have, then we need more information, such as what OS you are using and what compiler you have installed, etc.) – Dúthomhas Jan 18 '22 at 05:11
  • 1
    Not the current problem, but [watch out for identifiers like ` _b_h`](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). Doesn't bite all that often, but when it does, it's like the poor lass at the beginning of Jaws. – user4581301 Jan 18 '22 at 05:46
  • I don't know sublime text, but if you can get it to show you the full compiler and linker command lines, add them to the question. – user4581301 Jan 18 '22 at 05:49
  • @Dúthomhas I'm using macOS Monterey Version 12.1 (M1 chip). My compiler works fine and run successfully except when I try to include the header file as the example above. Here is my command to compile the file: ```g++ -std=c++17 -Wall a.cpp -o a``` – tobias Jan 18 '22 at 05:53
  • @user4581301 Thank you for your advice! I just put some information about the command line and g++ --version to the question. – tobias Jan 18 '22 at 06:07
  • It looks like you need to tell Sublime that `b.cpp` is also to be compiled and linked — it is missing from your command. Alas, I cannot help you on OS X. Someone else here may be better able to do that. – Dúthomhas Jan 18 '22 at 06:07
  • @Dúthomhas Oh, it just worked! I just put ```g++ -std=c++17 -Wall a.cpp b.cpp -o a```. Maybe I gonna try to find a more automatic way to do this. Thanks a lot! – tobias Jan 18 '22 at 06:12
  • [The canonical documentation for setting up Sublime Build Systems will help you do that.](https://www.sublimetext.com/docs/build_systems.html) – Dúthomhas Jan 18 '22 at 06:15

0 Answers0