wjkaufman
wjkaufman

Reputation: 53

Undefined Internal Linkage and Undefined Symbols Error

I tried compiling a simple program on Xcode and got the following messages:

function<anonymous namespace>::Initialize' has internal linkage but is not defined

function<anonymous namespace>::HandleBadInput' has internal linkage but is not defined

Undefined symbols for architecture x86_64:
  "(anonymous namespace)::Initialize()", referenced from:
      _main in main.o
  "(anonymous namespace)::HandleBadInput()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The header file looks like this:

#ifndef WJKErrorHandling
#define WJKErrorHandling

namespace WJKErrorHandling{

    void Initialize(void);
    int HandleBadInput(void);

}

#endif // defined(WJKErrorHandling)

the implementation file looks like this:

#include <iostream>
#include "WJKErrorHandling.h"

namespace WJKErrorHandling{

    void Initialize(void){

        std::cin.exceptions(std::cin.failbit);
    }

    int HandleBadInput(void){

        std::cerr << "Input Error: wrong type?\n";
        std::cin.clear();

        char BadInput[5];
        std::cin >> BadInput;

        return 1;
    }

}

and main.cpp looks like this:

#include <iostream>
#include "WJKErrorHandling.h"


void Prompt (void){

    //Prompts the user to begin entering numbers

    std::cout << "Begin entering numbers: \n";
}

float GetNumber (void){

    std::cout << "Number: \n";
    float Number;
    std::cin >> Number;
    return Number;
    }

std::string GetString (void){

    std::cout << "String: \n";
    std::string String;
    std::cin >> String;
    return String;

}

int main()
{
    Prompt();
    WJKErrorHandling::Initialize();

    int ReturnCode = 0;

    try{

        float Number = GetNumber();
        std::cout << Number;
        std::string String = GetString();
        std::cout << String;

        std::cout << "SUCCESS!!!!\n";

    }
    catch(...){

        ReturnCode = WJKErrorHandling::HandleBadInput();
    }


    return ReturnCode;
}

I've tried finding an answer so far, but I haven't understood any of the posts that I've found. I'm new with C++, so any help would be greatly appreciated!

Upvotes: 0

Views: 5045

Answers (3)

Micha Wiedenmann
Micha Wiedenmann

Reputation: 20843

You could also use the non-standard but more idiomatic #pragma once, according to its wikipedia page it is supported by all major compilers.

Since many compilers have optimizations to identify include guards, there is no speed advantage between the two. For myself I see the following advantages of #pragma once:

  • It has only one meaning (whereas defines serve different purposes) and will not clash with other things (e.g. a namespace as in your case).
  • It is little to type and simple to remember.
  • You cannot have errors due to a typo (WJKERRORHANDLNG_H, ups and I is missing), because you started the header as a copy of another and forgot to change the include guard, which gives you rather nasty bughunting sessions.

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340208

This turns out to be a bad include guard:

#ifndef WJKErrorHandling
#define WJKErrorHandling

because you later try to use WJKErrorHandling as a namespace, but the macro makes it go away.

Change your include guard to something like:

#ifndef WJKERRORHANDLING_H
#define WJKERRORHANDLING_H

which is probably more idiomatic and less likely to conflict with something.

Upvotes: 0

billz
billz

Reputation: 45410

Your #define Guard is causing name lookup issue.

change to below style should fix the issue:

#ifndef WJK_ERROR_HANDLING_H
#define WJK_ERROR_HANDLING_H

Upvotes: 4

Related Questions