简体   繁体   English

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

[英]CLion — Release build causing linker errors

I have a .cpp file and a corresponding .h inside a project in CLion like so: 我在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

These two files (along with others) are all built from a subdirectory into a .dylib which is then linked to the executable built by the main project. 这两个文件(以及其他文件)都从子目录构建到.dylib ,然后链接到由主项目构建的可执行文件。 This .dylib builds and links just fine under the Debug build, but when I switch to release build in the IDE, I get the following linker error: 这个.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

I don't know if I've broken something in my project or in the settings, but for some reason the Release build is failing. 我不知道我是否在项目或设置中破坏了某些内容,但由于某种原因,发布版本失败了。

The error message looks like your library was compiled with a different architecture and/or build settings than the other code in your project. 该错误消息看起来像您的库是使用与项目中其他代码不同的体系结构和/或构建设置编译的。

Of course, try cleaning the project first. 当然,请先尝试清洁项目。

You may need to configure your project such that: 您可能需要配置您的项目,以便:

  • In your debug build, you compile the library and the main with debug settings the same, and link with the same version of the standard C++ library. 在调试版本中,使用相同的调试设置编译库和主库,并使用相同版本的标准C ++库进行链接。
  • Similarly in release, the library and main code should be using the same release flags and link against the same version of the standard C++ libraries. 类似地,在发行版中,库和主代码应使用相同的发行标志,并链接到相同版本的标准C ++库。

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

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