0
votes

So I'm in an intro to Programming course in University and before anything else, let me say sorry for the poor formatting, I have no clue how to format this properly. But back to my questions, so we are primarily using c++ but I've been having some issues when I'm compiling my files. The compiler works for the most part and it will tell me when I have errors in my code but once I iron those out, it gives me a message saying that there are Undefined symbols for my x86_64 architecture.
I tried looking that up and found some things saying that Macs (I'm running Mavericks) sometimes have conflicting 32 bit and 64 bit libraries. When I first looked this up, I found some threads where people said to add the linker "-stdlib=libstdc++", but that did not work. I tried this in terminal using g++, but then my message gets worse. I've simplified most of my code to where the error pops up with minimal code.

Here is my main.cpp:

    #include <iostream>
    #include "helpers.h"
    #include <string>

    using namespace std;

    int main(){
        int num = 1;
        string y = "hi";
        hello(num, y);
    }

Here is my helpers.h

    #ifndef __helpers__
    #define __helpers__

    #include <string>
    #include <iostream>

    using namespace std;

    void hello(int number, string name);

    #endif

and finally here is my helpers.cpp:

    #include <iostream>

    using namespace std;

    void hello(int number, string name){
    return;
    }

When I compile it in g++, I get the following message:

Undefined symbols for architecture x86_64: "hello(int, std::__1::basic_string, std::__1::allocator >)", referenced from: _main in main-88f880.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

However, when I compile it in g++ with "g++ main.cpp -stdlib=libstdc++", I get the same message. I tried compiling it with clang, but I got a longer message:

"hello(int, std::string)", referenced from: _main in main-2806e5.o "std::allocator::allocator()", referenced from: _main in main-2806e5.o "std::allocator::~allocator()", referenced from: _main in main-2806e5.o "std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&)", referenced from: _main in main-2806e5.o "std::basic_string, std::allocator >::basic_string(std::string const&)", referenced from: _main in main-2806e5.o "std::basic_string, std::allocator >::~basic_string()", referenced from: _main in main-2806e5.o "std::ios_base::Init::Init()", referenced from: ___cxx_global_var_init in main-2806e5.o "std::ios_base::Init::~Init()", referenced from: ___cxx_global_var_init in main-2806e5.o "std::terminate()", referenced from: ___clang_call_terminate in main-2806e5.o "___cxa_begin_catch", referenced from: ___clang_call_terminate in main-2806e5.o "___gxx_personality_v0", referenced from: _main in main-2806e5.o Dwarf Exception Unwind Info (__eh_frame) in main-2806e5.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Thanks for your help.

1
__helpers__ is a reserved identifier.T.C.

1 Answers

4
votes

If you're only running g++ main.cpp, you're not going to compile helpers.cpp. Add helpers.cpp to the command line.