【问题标题】:CLion -- Release build causing linker errorsCLion -- 发布版本导致链接器错误
【发布时间】:2015-08-12 11:11:12
【问题描述】:

我在 CLion 的一个项目中有一个 .cpp 文件和一个对应的 .h,如下所示:

element.h

#pragma once

#include <string>
#include <unordered_map>

enum class Element
{
    H, He,
    Li, Be, B, C, N, O, F, Ne,
    Na, Mg, Al, Si, P, S, Cl, Ar,
    K, Ca
};

class ElementHash
{
// simple hash function in operator()
};

// LINE IN QUESTION:
std::ostream& operator<<(std::ostream& out, const Element& e);

struct ElementData
{
};

extern const std::unordered_map<std::string, Element> elementObjectLookupTable;
extern const std::unordered_map<Element, ElementData, ElementHash> elementDataLoopkupTable;

std::string toString(const Element& e);

element.cpp

#include "element.h"

using namespace std;


ostream& operator<<(ostream& out, const Element& e)
{
    out << toString(e);
    return out;
}

// rest of the file's not important

这两个文件(以及其他文件)都从一个子目录构建到 .dylib 中,然后链接到主项目构建的可执行文件。这 .dylib 在 Debug 构建下构建和链接很好,但是当我在 IDE 中切换到发布构建时,我收到以下链接器错误:

Undefined symbols for architecture x86_64:
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Element const&) in element.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [/Users/_____/ClionProjects/chemhelp/bin/Release/libchemhelp-core.dylib] Error 1
make[2]: *** [CMakeFiles/chemhelp-core.dir/all] Error 2
make[1]: *** [CMakeFiles/chemhelp-core.dir/rule] Error 2
make: *** [chemhelp-core] Error 2

我不知道我是否在我的项目或设置中破坏了某些东西,但由于某种原因,发布版本失败了。

【问题讨论】:

    标签: c++ linker linker-errors clion


    【解决方案1】:

    错误消息显示,您的库是使用与项目中其他代码不同的架构和/或构建设置编译的。

    当然,先尝试清理项目。

    您可能需要这样配置您的项目:

    • 在您的调试版本中,您使用相同的调试设置编译库和主库,并链接到相同版本的标准 C++ 库。
    • 与发布类似,库和主代码应使用相同的发布标志并链接到相同版本的标准 C++ 库。

    【讨论】:

    • 在“Edit Configurations”设置对话框中,我将“Build All”设置为“Release”,“chemhelp”(可执行文件)设置为“Release”,“chemhelp-core”(dylib)设置为“发布”。我仍然收到相同的链接器错误。当我将所有 3 设置为“调试”时,我没有收到任何错误。
    • 我仍然会仔细检查发布设置中每个组件的 C++ 标准库版本。还要检查所有优化标志等。大多数时候我看到这些奇怪的地方是库和链接到该库的可执行文件之间的设置不匹配......
    • 您可能需要检查您的 CMake 文件,以确保您的库和构建设置在发布和调试构建的各个组件之间保持一致。
    猜你喜欢
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多