2

I have three C++ files and I'm getting a very annoying C++ linker error. Here's the error:

Undefined symbols for architecture x86_64:
  "tiled::debug::log(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
  _main in main.o
  error_callback(int, char const*) 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)

Here are my files:

main.cpp

#include "debug.hpp"
#include <GLFW/glfw3.h>

using namespace tiled;

void error_callback(int error, const char* description);

int main(int argc, char* argv[])
{
    debug::log("Initializing");
    glfwSetErrorCallback(error_callback);

    debug::log("Initializing GLFW...");
    if (!glfwInit())
    {
        debug::log("Failed to initialize GLFW!");

        glfwTerminate();
        return -1;
    }
    debug::log("Done");

    debug::log("Exiting program!");
    glfwTerminate();
    return 0;
}

void error_callback(int error, const char* description)
{
    std::string error_code;
    error_code.append("GLFW error ");
    error_code.append(std::to_string(error));

    debug::log(error_code);
    debug::log(description);
}

debug.hpp

#ifndef TILED_DEBUG_HPP
#define TILED_DEBUG_HPP

#include <ctime>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

namespace tiled
{
    class debug {
    private:
        static std::vector<std::string> data;
        static std::ofstream log_file;

    public:
        static void log(const std::string& msg);
        static int write_logs();
    };
}

#endif /* TILED_DEBUG_HPP */

and, debug.cpp

#include "debug.hpp"

namespace tiled
{
    void debug::log (const std::string& msg)
    {
        // Log to the console
        std::cout << msg << std::endl;
        // Add to the data we will put in the log file.
        data.push_back(msg);
    }

    int debug::write_logs()
    {
        std::string data_str;

        /*
         We get the system time here. This is because we want to name each log
         after what time the log file was made.
         */
        std::time_t result = std::time(nullptr);
        std::string time = std::asctime(std::localtime(&result));

        data_str.append("\n");
        data_str.append(time);

        // Put the contents of the data vector into the string we append to file
        for (std::string str : data)
        {
            data_str.append("\n");
            data_str.append(str);
        }

        log_file.open(time.c_str(), std::ios::app);

        if (log_file.is_open())
        {
            log_file << data_str;
            log_file.close();
        }
        else
        {
            std::cout << "Error: Couldn't write log file!" << std::endl;
            return 1;
        }

        return 0;
    }
}

It seems like the function log is not defined, but I defined it in the debug.cpp file. I've been working at this for hours... Can anyone help me?

Also, If it helps here are the commands Xcode is using the build the program:

Ld /Users/home/Library/Developer/Xcode/DerivedData/tiled-bhqwfqpsuugzhkagbmrhebnbgnpb/Build/Products/Debug/tiled normal x86_64 cd /Users/home/Documents/tiled export MACOSX_DEPLOYMENT_TARGET=10.10 /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/Users/home/Library/Developer/Xcode/DerivedData/tiled-bhqwfqpsuugzhkagbmrhebnbgnpb/Build/Products/Debug -L/usr/local/lib -F/Users/home/Library/Developer/Xcode/DerivedData/tiled-bhqwfqpsuugzhkagbmrhebnbgnpb/Build/Products/Debug -filelist /Users/home/Library/Developer/Xcode/DerivedData/tiled-bhqwfqpsuugzhkagbmrhebnbgnpb/Build/Intermediates/tiled.build/Debug/tiled.build/Objects-normal/x86_64/tiled.LinkFileList -mmacosx-version-min=10.10 -stdlib=libc++ -lglfw.3.1 -framework OpenGL -Xlinker -dependency_info -Xlinker /Users/home/Library/Developer/Xcode/DerivedData/tiled-bhqwfqpsuugzhkagbmrhebnbgnpb/Build/Intermediates/tiled.build/Debug/tiled.build/Objects-normal/x86_64/tiled_dependency_info.dat -o /Users/home/Library/Developer/Xcode/DerivedData/tiled-bhqwfqpsuugzhkagbmrhebnbgnpb/Build/Products/Debug/tile

Ben
  • 61
  • 1
  • 9
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Captain Obvlious Jul 25 '15 at 00:21
  • Can you give us what ide you are using, or what you are using for you command-line build parameters? – Kevin Jul 25 '15 at 00:26
  • Yeah! I'll add that in to the question. – Ben Jul 25 '15 at 00:32

1 Answers1

0

To fix this I got rid of the .cpp file and just made those inline functions. Because this was just a workaround, I'm not going to select this as an answer.

Ben
  • 61
  • 1
  • 9
  • 1
    If you get rid of the debug.cpp and make those function as inline in debug.hpp and the error disappears, that means your build system linker does not know how to find the debug.obj, or the debug.cpp wasn't compiled. This could be caused by improper setup of your project in the IDE you are using. What IDE you are using and how does your project and files created and structured? – simon Jul 25 '15 at 06:08
  • I'm using Xcode and you're right, debug.cpp wasn't compiled. However when I add it back in, I get a duplicate symbols linker error. – Ben Jul 25 '15 at 07:10
  • 1
    You added back the debug.cpp. Did you remove the inlined implemention in debug.h? If not, it will give you duplicated symbol in linking as you are having two implementation now. Does your main and your debug.cpp sitting on the same project or different project? If different project, have you properly setup project reference or library path? – simon Jul 25 '15 at 08:14