簡體   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