【发布时间】:2016-09-29 05:10:08
【问题描述】:
我有 3 个文件
测试.h
#pragma once
#include <memory>
std::unique_ptr<int> createInt();
test.cpp - 注意 test.h 不包括在内
#include <memory>
std::shared_ptr<int> createInt()
{
return std::make_shared<int>();
}
main.cpp
#include "test.h"
int main()
{
createInt();
return 0;
}
它编译没有任何问题
g++ -Wall -Wextra main.cpp test.cpp
并在运行时失败:
./a.out
a.out(3632,0x7fff78ba9300) malloc: *** error for object 0x7fe290404c68: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
但是 Visual Studio 不会编译此类源代码并出现 LNK1120 错误。
为什么 g++ 链接器不会失败这样的代码?
【问题讨论】: