當我嘗試編譯下面的代碼時,出現鏈接器錯誤:Undefined symbols for architecture x86_64: "Foo()", referenced from: _main in main.o
使用LLVM 4.2。未定義constexpr函數的符號
此功能僅在功能標記爲constexpr
時發生。當功能標記爲const
時,程序將正確編譯和鏈接。爲什麼聲明函數constexpr
會導致鏈接器錯誤?
(我知道寫的函數這種方式不給編譯時計算的好處。在這一點上我很好奇,爲什麼函數失敗鏈接)
的main.cpp
#include <iostream>
#include "test.hpp"
int main()
{
int bar = Foo();
std::cout << bar << std::endl;
return 0;
}
test.hpp
constexpr int Foo();
TEST.CPP
#include "test.hpp"
constexpr int Foo()
{
return 42;
}
見http://stackoverflow.com/questions/14391272/does-constexpr-imply-inline – 2013-04-25 16:12:47