繁体   English   中英

CLion —发行版本引起链接器错误

[英]CLion — Release build causing linker errors

我在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 ++库进行链接。
  • 类似地,在发行版中,库和主代码应使用相同的发行标志,并链接到相同版本的标准C ++库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM