Cannot figure out a very basic issue...

Hello everyone.

I've been dealing with this issue for over a month on my M1 MBP. I'm using CLion for C++. I cannot get any program I write to compile with g++/gcc. I have taken the following steps to remedy the issue: Update CLion, Reinstall CLion, Update and Reinstall CommandLineTools, Reinstall MacOS, reset the computer to default.

I've tried everything I can think of to get this to behave how it was originally. The issue started when I updated to MacOS Ventura. I deeply, deeply regret upgrading at this point but have no way of going back.

This is a very basic program I've written to demonstrate the issue.


int main() {

    welcome();

}

void welcome(){

    std::cout << "Hello world";

}
void welcome();

Very, very basic. The issue is that every single time I try to do g++ main.cpp, I get the following error.

  "welcome()", referenced from:
      _main in main-fbb48c.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Replies

I can’t help you with third-party IDEs but here’s an example of how to build a trivial C++ program from the command line:

% cat main.cpp                        
#include "welcome.h"

int main() {
    welcome();
}
% cat welcome.h                       
void welcome();
% cat welcome.cpp                     
#include "welcome.h"

#include <iostream>

void welcome(){
    std::cout << "Hello Cruel World!";
}
% clang main.cpp welcome.cpp -l stdc++
% ./a.out
Hello Cruel World!

It seems that you’re passing the compiler driver just your main.cpp file, but it needs both that and welcome.cpp so that it can build both files and link together the results.

Share and Enjoy

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

We can't tell what you're doing wrong without seeing how you invoked the compiler, but Quinn is probably right that you've not linked with the file with the implementation of your welcome() function.

A couple of other things to note:

  1. You're probably not using g++/gcc. Apple alias g++ and gcc to clang++ and clang. This has some advantages, i.e. some things "just work", but I think it also has the potential to cause some confusion.

  2. Put \n at the end of your "Hello World" string.

  • I'm using CLion, so would the apple alias for g++ to clang still apply? The thing is, at the time this issue arose, Codecademy also started having issues with their terminal too. I cannot use g++ on there either. I did put a \n.

    In the compiler I typed g++ main.cpp and that's the error it gave.

Add a Comment