【问题标题】:How to compile visual C++ program with additional libraries如何使用附加库编译可视化 C++ 程序
【发布时间】:2013-12-19 05:50:31
【问题描述】:

我定义了源和标题 -“MathCore.h”和“MathCore.cpp” MathCore.h 如下代码:

#ifdef MATHCORE_EXPORTS
#define MATHCORE_API __declspec(dllexport)
#else
#define MATHCORE_API __declspec(dllimport)
#endif

MATHCORE_API bool isPowOf_Two(unsigned int n);
MATHCORE_API bool isFormOf_tpnmo(unsigned int n);
MATHCORE_API int isolate_LST( int x);


// This class is exported from the MathCore.dll
class MATHCORE_API MathEngine {
public:
    MathEngine(void);
    // TODO: add your methods here.
};

extern MATHCORE_API int nMathCore;

MATHCORE_API int fnMathCore(void);

我从 Visual Studio 2008 生成了 lib -“MathCore.lib”和相应的 dll-“MathCore.dll”,然后我在另一个使用 MathCore 中定义的函数的文件夹中创建了 C++ 源文件 -“t.cpp”,为简单起见,我将 MathCore.lib 和 MathCore.dll 放在同一个文件夹中。 t.cpp如下

#pragma comment(lib, "MathCore.lib")
#include <iostream>
#include "MathCore.h"

using namespace std;

MATHCORE_API bool isPowOf_Two(unsigned int n);
MATHCORE_API bool isFormOf_tpnmo(unsigned int n);

int main()
{

    while(1){
    unsigned int x;
    cin>>x;
    cout<<isPowOf_Two(x)<<"\n";
    cout<<x<<"\n";
    }
    system("Pause");
    return 0;
}

我的问题是.. 不使用 Visual Studio 我想使用批处理文件编译 t.cpp -"run.bat" 所以我将“vcvarsall.bat”和“vcvars32.bat”包含在同一个源文件夹中,我的“run.bat”命令是这样的......

@echo off
call "vcvarsall.bat"
call "vcvars32.bat"
echo off
cl /O2 t.cpp /link MathCore.lib
@if ERRORLEVEL == 0 (
goto good
)

@if ERRORLEVEL != 0 (
goto bad
)

:good
echo "clean compile"
echo %ERRORLEVEL%
goto end

:bad
echo "error or warning"
echo %ERRORLEVEL%
goto end

:end

当我运行“run.bat”时,它会创建没有“t.exe”的“t.obj”命令行 我检查了 Visual Studio 命令行争论这对我解决这个问题没有帮助。

请任何人知道使用所需库和头文件编译可视 C++ 源文件的确切命令行参数 请参考http://msdn.microsoft.com/en-us/library/610ecb4h.aspx

【问题讨论】:

  • 当您使用 Visual Studio 进行标记时,MSBuild 可能更容易。您可以配置项目属性以使其在 IDE 中编译,然后在之后自动构建。
  • 我想在不使用 Visual Studio 的情况下编译源代码,我想执行一个 bat 并做这些事情..请帮助.. 链接库问题我参考了这些东西 msdn.microsoft.com/en-us/library/610ecb4h.aspx
  • 这就是 MSBuild 所做的 - 您根本不运行 IDE,您可以从 .bat 文件构建。
  • 请您给我发送有用的链接如何使用 MSBuild 实现它,我对构建东西的经验较少,我认为 MSBuild 使用 Visual Studio 属性文件(props,sln)和其他东西?
  • 该链接在我的第一条评论中 - 但如果您可以通过 Visual Studio 构建,您基本上只需运行 MSBuild 并将其命名为 .sln。

标签: c++ visual-c++ visual-c++-2010 visual-c++-2008


【解决方案1】:

不知道实际错误会很难说。

我创建了一个示例库并尝试链接它。为了让它工作,我改变了下面的行。

cl /O2 t.cpp /link MathCore.lib

cl /O2 t.cpp /MDd MathCore.lib

如果您运行 *.bat(在您的情况下为 run.bat)并将错误粘贴到此处会很好。

[PS:我无法添加评论因此添加回复]

【讨论】:

  • 感谢您的回答。问题是当我删除 "cout
  • 您是否尝试从调用者函数中删除以下行? MATHCORE_API bool isPowOf_Two(unsigned int n); MATHCORE_API bool isFormOf_tpnmo(unsigned int n);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多