extern "C" ,const type& fun() const

1.extern "C" 和dllexport一起使用

库工程GlobalTest的.h代码如下

#ifdef GLOBALTEST_EXPORTS
#      define GT_EXPORT __declspec(dllexport)
#   
else
#      define GT_EXPORT __declspec(dllimport)
#   endif 
    

namespace tt
{
extern "C"  GT_EXPORT void fatalerr(void);
}

.cpp

#include "globalMethod.h"

#include 
<iostream>

using namespace tt;
void GT_EXPORT fatalerr(void)
{
    printf(
"fatal error: \n");
    
//exit(0);


application工程的main.cpp 

#pragma comment(lib, "GlobalTest.lib")
#include 
"../GlobalTest/globalMethod.h"

#include 
<iostream>

using namespace tt;
int main()
{
    fatalerr();
    system(
"PAUSE");
    
return 0;


这样是会报连接错误

main.obj : error LNK2019: 无法解析的外部符号 __imp__fatalerr,该符号在函数 _main 中被引用 

如果不使用namespace就不会。不解。 

2.const type& fun() const和type& fun(),用的甚好,无甚笔记。(使用时需留意const 指针)

原文地址:https://www.cnblogs.com/mumuliang/p/2074783.html