关键词:
【中文标题】在 Visual Studio 中编译与在命令行中使用 cl 编译【英文标题】:compiling in Visual Studio vs. compiling with cl in the command line 【发布时间】:2017-12-13 20:17:21 【问题描述】:我有一个用于在大学课程中测试 C 程序的脚本。我使用 VS2015 的开发人员命令提示符编译学生的文件,只需运行命令 cl file.c
。但是,我发现在某些(非常罕见的)情况下,与在 VS2015 上运行这些程序相比,我得到不同的输出(我们指导学生在该 IDE 中检查他们的程序)。 经过一番调查,我发现当我包含头 (.h) 文件时会发生这种情况(请参见下面的代码示例)。
cl
实际上是 VS2015 使用的同一个编译器吗?如果是 - 那我怎么会得到不同的结果。如果不是 - 我应该给出什么命令才能获得类似的结果?
代码示例 - 运行应评估数学字符串的函数的程序。评估是从左到右进行的,而不考虑普通的数学顺序优先级:
test_data.h
#ifndef _TEST_DATA_H
#define _TEST_DATA_H
char *mathString2 = "64 / 8 * 8 - 8 / 8 / 8 / 8 * 8 * 8 * 8";
#endif /* _TEST_DATA_H */
test.c
#include <stdio.h>
#include "test_data.h"
// Q1
double string2uint(char str[])
int i = 0;
double res = 0;
while (str[i] != '\0')
res = res * 10 + (str[i] - '0');
i++;
return res;
void calcMathString(char mathString[])
int i, j = 0;
double e = 0;
char t = '+', str_n[101];
for (; mathString[j] != '\0'; j++)
i = 0;
while (mathString[j] != '\0')
str_n[i] = mathString[j];
if (mathString[j] == ' ')
str_n[i] = '\0';
i++;
j++;
break;
if (mathString[j + 1] == '\0')
i++;
str_n[i] = '\0';
j++;
break;
i++;
j++;
if (t == '+')
e = e + string2uint(str_n);
if (t == '-')
e = e - string2uint(str_n);
if (t == '*')
e = e*string2uint(str_n);
if (t == '/')
e = e / string2uint(str_n);
t = mathString[j];
j = j + 1;
printf("%s = %.3lf\n", mathString, e);
int main()
char *mathString1 = "64 / 8 * 8 - 8 / 8 / 8 / 8 * 8 * 8 * 8";
calcMathString(mathString1);
calcMathString(mathString2);
return 0;
注意mathString1, mathString2
是相同的。
我用cl
编译的输出:
64 / 8 * 8 - 8 / 8 / 8 / 8 * 8 * 8 * 8 = 56.000
64 / 8 * 8 - 8 / 8 / 8 / 8 * 8 * 8 * 8 = 48.000
我在 VS 中运行时得到的输出:
64 / 8 * 8 - 8 / 8 / 8 / 8 * 8 * 8 * 8 = 56.000
64 / 8 * 8 - 8 / 8 / 8 / 8 * 8 * 8 * 8 = 56.000
【问题讨论】:
VS 有很多项目设置,您可能希望通过特定的cl
参数来模仿这些设置,以获得完全相同的二进制文件。
我如何知道这些参数?我希望我的命令行 cl
编译的行为与 VS 一样。
但这并不重要,因为不同的结果表明代码中有一些未定义的行为,应该修复。
尝试char *mathString2 = "64 / 8 * 8 - 8 / 8 / 8 / 8 * 8 * 8 * 8\0" "1111";
查看代码是否越界访问。
if (mathString[j + 1] == '\0')
可以访问字符串外部,因为之前的指令已经增加了 j
。用一个空格结束你的字符串,你可能会遇到这个问题。
【参考方案1】:
VS 运行与从命令提示符获得的相同的 cl.exe 编译器,但是在 VS 中构建几乎肯定会传递更多的命令行参数。
在命令行中使用“msbuild”会产生与 VS 相同的构建顺序。
【讨论】:
msbuild
无法直接编译单个 .c 文件。据我了解-它编译VS项目文件(对吗?)。如果是这样的话 - 它不会帮助我Visual Studio 在命令行中定义预处理器指令
】VisualStudio在命令行中定义预处理器指令【英文标题】:VisualStudiodefinepre-processordirectiveinthecommandline【发布时间】:2016-01-2520:43:14【问题描述】:#ifdefDEBUG_TESTstd::cout<<"Hello"<<std::endl;#endif如何在VS中将#defineDEBUG_TEST作为命... 查看详情
SSIS 包在 Visual Studio 和命令行中有效,但在代理中无效
】SSIS包在VisualStudio和命令行中有效,但在代理中无效【英文标题】:SSISpackageworksinVisualStudio,ANDCommandlinebutnotinAgent【发布时间】:2015-04-2516:41:38【问题描述】:我有一个使用VS在服务器上开发的SSIS包。该包在VisualStudio中运行良好... 查看详情
C++ -- 代码在 Visual Studio 中完美运行,但命令行中的 a.exe 不断崩溃
】C++--代码在VisualStudio中完美运行,但命令行中的a.exe不断崩溃【英文标题】:C++--CoderunsperfectlyinVisualStudio,buta.exeinthecommandlinekeepscrashing【发布时间】:2018-03-1907:30:20【问题描述】:我制作了一个使用链表来模拟内存管理的程序,... 查看详情
当我仅使用链接器选项 /LTCG 时,为啥 Visual Studio 会在链接器命令行中显示选项 /PGD?
】当我仅使用链接器选项/LTCG时,为啥VisualStudio会在链接器命令行中显示选项/PGD?【英文标题】:WhydoesVisualStudioshowtheoption/PGDinthelinkercommandlinewhenIusejustthelinkeroption/LTCG?当我仅使用链接器选项/LTCG时,为什么VisualStudio会在链接器命... 查看详情
在 Visual Studio 代码终端中运行 JavaScript 代码,与在浏览器控制台中运行的相同
】在VisualStudio代码终端中运行JavaScript代码,与在浏览器控制台中运行的相同【英文标题】:RunJavaScriptcodesinVisualStudiocodeterminalsameaswhatworksinbrowser\'sconsole【发布时间】:2020-12-2615:23:58【问题描述】:这是一个基本的vs代码问题,但... 查看详情
awk 在命令行中的行为与在脚本中的行为不同
】awk在命令行中的行为与在脚本中的行为不同【英文标题】:awkbehavesdifferentlyoncommand-linethanfromscript【发布时间】:2012-04-2501:18:59【问题描述】:我希望这会放置“1”,但它会输出“123”回声“123”|awk"打印$1"但是,当从文件运... 查看详情
通过命令行在 Visual Studio 中编译单个独立文件
】通过命令行在VisualStudio中编译单个独立文件【英文标题】:Compileasinglestandalonefileinvisualstudioviaitscommandline【发布时间】:2020-06-1914:56:18【问题描述】:我只是想检查我的独立文件是否可以编译。这通过使用内置VisualStudio(2020)命... 查看详情
为啥发布运行的代码与在 Visual Studio 中运行不同
】为啥发布运行的代码与在VisualStudio中运行不同【英文标题】:WhydoespublishrundifferentcodethanRunninginVisualStudio为什么发布运行的代码与在VisualStudio中运行不同【发布时间】:2022-01-2302:45:34【问题描述】:我有一个现有的网络应用程... 查看详情
Visual Studio 2019:使用 vcvars64.bat 从命令行构建 C++ 不再起作用
】VisualStudio2019:使用vcvars64.bat从命令行构建C++不再起作用【英文标题】:VisualStudio2019:buildC++fromcommandlinewithvcvars64.batdoesn\'tworkanymore【发布时间】:2020-01-0918:33:50【问题描述】:为了在以前版本的VisualStudio的命令行中使用clcpp编译... 查看详情
Tkinter 程序在 IDE (Visual Studio) 中运行良好,但是当使用 pyinstaller 编译为 .exe 时,线程的工作方式与在 IDE 中不同
】Tkinter程序在IDE(VisualStudio)中运行良好,但是当使用pyinstaller编译为.exe时,线程的工作方式与在IDE中不同【英文标题】:TkinterprogramworksfineinIDE(VisualStudio)butwhenusingpyinstallertocompileto.exethreadingdoesnotworksamelikeitdoesinIDE【发布时间】:2... 查看详情
在 Powershell 中运行时与在 Visual Studio 中运行时的 HttpClient 并发行为不同
】在Powershell中运行时与在VisualStudio中运行时的HttpClient并发行为不同【英文标题】:HttpClientconcurrentbehaviordifferentwhenrunninginPowershellthaninVisualStudio【发布时间】:2020-06-2716:13:27【问题描述】:我正在使用MSGraphAPI将数百万用户从本地... 查看详情
qmake 可以在 Visual Studio 项目中配置命令行选项吗?
】qmake可以在VisualStudio项目中配置命令行选项吗?【英文标题】:CanqmakeconfigurecommandlineoptionsinaVisualStudioproject?【发布时间】:2013-02-0621:01:34【问题描述】:我有兴趣在我使用qmake创建的每个VisualStudio项目中使用/MP作为命令行选项... 查看详情
在gcc中定义符号常量
http://www.ha97.com/2830.html还可以在编译命令行中定义符号常量。为此,我们可以简单的在命令行中使用-D选项即可,如下例所示:$gcc-DTEST_CONFIGURATIONtest.c-otest上面的命令与在源文件中加入下列命令是等效的:#defineTEST_... 查看详情
在 Visual Studio 2008 中没有足够的存储空间来处理此命令
】在VisualStudio2008中没有足够的存储空间来处理此命令【英文标题】:NotenoughstorageisavailabletoprocessthiscommandinVisualStudio2008【发布时间】:2010-11-0300:18:48【问题描述】:当我尝试在VS2008中编译程序集时,我得到了(偶尔,通常在项目... 查看详情
Visual Studio 中重写的环境变量?
】VisualStudio中重写的环境变量?【英文标题】:EnvironmentvariableoverriddeninVisualStudio?【发布时间】:2014-10-0211:15:15【问题描述】:我正在使用VisualStudioExpress2013并尝试编译其他人的代码。文档告诉我在命令行中设置环境变量QTDIR,然... 查看详情
如何从命令行编译 Visual Studio 项目?
】如何从命令行编译VisualStudio项目?【英文标题】:HowdoIcompileaVisualStudioprojectfromthecommand-line?【发布时间】:2010-10-0415:05:48【问题描述】:我正在为使用Monotone、CMake、VisualStudioExpress2008和自定义测试的大型C++解决方案编写签出、... 查看详情
Visual Studio 2010 像 Visual Studio 6 一样在 C++ 中编译内联程序集?
】VisualStudio2010像VisualStudio6一样在C++中编译内联程序集?【英文标题】:VisualStudio2010compilinginlineassemblyinc++asifVisualStudio6?【发布时间】:2011-01-0715:17:34【问题描述】:我有一个在VS6中创建的C++项目,它在VS2010中打开并且编译良好... 查看详情
我可以从命令行编码和编译所有类型的 Visual Studio 应用程序吗?
】我可以从命令行编码和编译所有类型的VisualStudio应用程序吗?【英文标题】:CanIcodeandcompilealltypeofVisualStudioApplicationfromCommandline?【发布时间】:2011-02-1612:31:29【问题描述】:我正处于使用Emacs作为我的编程环境的第一步。我使... 查看详情