[英]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
我不知道我是否在項目或設置中破壞了某些內容,但由於某種原因,發布版本失敗了。
該錯誤消息看起來像您的庫是使用與項目中其他代碼不同的體系結構和/或構建設置編譯的。
當然,請先嘗試清潔項目。
您可能需要配置您的項目,以便:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.