如何使用其他库编译可视化C ++程序 [英] How to compile visual C++ program with additional libraries

查看:127
本文介绍了如何使用其他库编译可视化C ++程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了来源&标题 - 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);


// 此类从MathCore.dll导出
class MATHCORE_API MathEngine {
public
MathEngine(< span class =code-keyword> void );
// TODO:在此处添加您的方法。
};

extern MATHCORE_API int nMathCore;

MATHCORE_API int fnMathCore( void );



我从visual studio 2008生成了lib - MathCore.lib和各自的dll-MathCore.dll,然后我在一个不同的文件夹中创建了C ++源文件 - t.cpp在MathCore中定义的函数,为简单起见,我将MathCore.lib和MathCore.dll放在同一个文件夹中。

t.cpp就像这样....

  #pragma comment(lib,MathCore.lib)
#include < span class =code-preprocessor> < iostream >
#include MathCore.h

使用 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( 暂停);
return 0 ;
}





我的问题是..不使用Visual Studio我想用批处理文件编译t.cpp说 - run.bat

所以我将vcvarsall.bat和vcvars32.bat包含在同一个源文件夹中,我的run.bat命令就像这样....

 @ echo off 
callvcvarsall.bat
callvcvars32.bat
echo off
cl / O2 t.cpp / link MathCore.lib
@if ERRORLEVEL == 0(
goto good


@if ERRORLEVEL!= 0(
goto bad


:好
echoclean compile
echo%ERRORLEVEL%
goto end

:bad
echo错误或警告
echo%ERRORLEVEL%
转到结束

:结束





当我运行run.bat它创建t.obj没有t.exe我认为它没有链接MathCore.lib,我想知道如何使用其他库编译可视化C ++源代码并包含在命令行我chekc视觉工作室命令行辩论它没有帮助我解决这个问题。



请任何人都知道使用所需的libs文件编译可视化c ++源文件的确切命令行论证。

解决方案

我会用

 CL / O2 t.cpp MathCore.lib 


参见 http://msdn.microsoft.com/en-us/library/610ecb4h (v = vs.100).aspx [ ^ ]以及 / OUT 选项-us /库/ y0zzbyt4(v = VS.100)的.aspx> http://msdn.microsoft.com/en-us/library/y0zzbyt4(v = VS.100)的.aspx [<一个href =http://msdn.microsoft.com/en-us/library/y0zzbyt4(v=vs.100).aspx\"target =_ blanktitle =New Window> ^ ]。

I defined source & header -"MathCore.h" & "MathCore.cpp"
MathCore.h as below code :

#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);


and I generated lib - "MathCore.lib" and respective dll-"MathCore.dll" from visual studio 2008,then I created C++ source file-"t.cpp" in a different folder which uses functions defined in MathCore,for simplicity I put MathCore.lib and MathCore.dll within same folder.
t.cpp like this....

#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;
}



My problem is.. without using Visual Studio I want to compile the t.cpp using batch file say -"run.bat"
so I included "vcvarsall.bat" and "vcvars32.bat" to same source folder,my "run.bat" commands like this ....

@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



when I run "run.bat" it creates "t.obj" without "t.exe" I think it does not link MathCore.lib,I want to know how to compile visual C++ source with additional libraries and includes in commandline I chekc visual studio commandline arguements It not helped me to solve this problem.

Please anyone know exact commandline arguements to compile a visual c++ source file with needed libs files.

解决方案

I would use

CL /O2 t.cpp MathCore.lib


See http://msdn.microsoft.com/en-us/library/610ecb4h(v=vs.100).aspx[^] and also the /OUT option at http://msdn.microsoft.com/en-us/library/y0zzbyt4(v=vs.100).aspx[^].


这篇关于如何使用其他库编译可视化C ++程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆