如何解决“不一致的DLL链接(C4273)”警告?

时间:2019-05-07 07:15:22

标签: c++ visual-studio dll dllimport dllexport

我在这里阅读了各种“不一致的dll链接”问题和答案,但都没有解决一般问题,它们要么是针对特定代码解决问题,要么是使用Visual Studio。

我在Visual Studio Developer提示符下使用cl.exe程序。我正在按照演练在Visual Studio here

中创建一个dll。

我的文件MathLibrary.h

#pragma once
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
extern "C" MATHLIBRARY_API void fibonacci_init(
    const unsigned long long a, const unsigned long long b);
extern "C" MATHLIBRARY_API bool fibonacci_next();
extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();
extern "C" MATHLIBRARY_API unsigned fibonacci_index();

并在文件MathLibrary.cpp

#include <utility>
#include <limits.h>
#include "MathLibrary.h"
static unsigned long long previous_;  
static unsigned long long current_;
static unsigned index_;    
void fibonacci_init(
    const unsigned long long a,
    const unsigned long long b)
{
    index_ = 0;
    current_ = a;
    previous_ = b; 
}
bool fibonacci_next()
{
   if ((ULLONG_MAX - previous_ < current_) ||
       (UINT_MAX == index_))
    {
        return false;
    }
    if (index_ > 0)
    {
        // otherwise, calculate next sequence value
        previous_ += current_;
    }
    std::swap(current_, previous_);
    ++index_;
    return true;
}
unsigned long long fibonacci_current()
{
    return current_;
}
unsigned fibonacci_index()
{
    return index_;
}

现在,我在开发者提示符下使用cl.exe程序,例如

cl /LD MathLibrary.cpp

由于文件MathLibrary.h位于同一目录中,因此不会造成任何问题。我收到一堆“ C4273”警告。它们是“不一致的dll链接”警告。

我创建了一个MathClient.cpp文件

#include <iostream>
#include "MathLibrary.h"
int main()
{
    fibonacci_init(1, 1);
    do {
        std::cout << fibonacci_index() << ": "
            << fibonacci_current() << std::endl;
    } while (fibonacci_next());
    std::cout << fibonacci_index() + 1 <<
        " Fibonacci sequence values fit in an " <<
        "unsigned 64-bit integer." << std::endl;
}

这完美无瑕,所以我知道没有任何致命的错误,只有这些警告。我已经读到它是在未定义MATHLIBRARY_EXPORTS且__declspec(dllimport)与函数定义一起出现时生成的。

我不确定如何解决此问题,因为我正在使用命令行(开发人员提示符),并且无法设置预处理程序定义

0 个答案:

没有答案