C ++ - 未定义的符号链接器错误 - Xcode

时间:2015-07-25 00:17:57

标签: c++ xcode linker

我有三个C ++文件,而且我得到了一个非常烦人的C ++链接器错误。这是错误:

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)

以下是我的文件:

的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 */

和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;
    }
}

似乎函数日志没有定义,但是我在debug.cpp文件中定义了它。我已经在这工作了几个小时......任何人都可以帮助我吗?

此外,如果它有帮助,Xcode正在使用构建程序的命令:

Ld / Users / home / Library / Developer / Xcode / DerivedData / tiled-bhqwfqpsuugzhkagbmrhebnbgnpb / Build / Products / Debug / tiles normal x86_64     cd / Users / home / Documents / tiles     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 / tiles-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-bhqwfqpsuugzhka gbmrhebnbgnpb /生成/产品/调试/瓦

1 个答案:

答案 0 :(得分:0)

为了解决这个问题,我摆脱了.cpp文件,并制作了这些内联函数。因为这只是一种解决方法,所以我不打算选择这个作为答案。