注:感谢您能点开本文,本文是我 debug 后的随笔,可能没那么容易读懂(现在是0:44...实在太累了...写不动具体场景了)。待到我熬过这段...将写出更多深入浅出的好文与您分享!
我这人有个毛病:一定要用 VS Code
写代码,确切地说,一定要用我自己的 VS code
,自己配好的键盘映射、自己的插件。
近来从实习岗位回学校,想要在自己的 win 10 上跑通实验代码,何谈容易。最主要的问题在于:我需要用到外部依赖 gecode
,对于 win 10 ,经验告诉我,从源码编译来安装并不是一个好的选择,从官网下载 .msi
安装包再配合 VC++
是明智之举。我没什么意见,但是...我不想用 VS(VC++的编辑器),只想用 VS Code 。这就需要额外学习一点东西了:
MSVC
cl.exe
使用?首先我们可以得到 cl.exe 编译链接的方法,由 gecode说明书MPG[1] :
cl /DNDEBUG /EHsc /MD /wd4355 -I "D:\Program Files\gecode\include" \
-c ./$filename.obj -Tp ./$filename.cpp
cl /DNDEBUG /EHsc /MD /wd4355 -I "D:\Program Files\gecode\include" \
-Fe ./$filename.obj /link /LIBPATH:"D:\Program Files\gecode\lib"
如上,先编译为 .obj ,再链接,链接时别忘了告诉编译器 *.lib 都放在哪里。
我们不可能每次运行程序时,都向命令行输入这么长一串命令,于是我想到:
include
和 lib
里?(污染系统环境,且我们不可能每应用一个外部依赖,就修改一次系统环境,遂放弃)于是就有了 test.ps1
:
$filename = main
if ($args.Count -eq 1)
{
$filename = $args[0]
}
else
{
Write-Output "There must be one filename as arg!"
exit
}
if (Test-Path ./$filename.obj)
{
Remove-Item ./$filename.obj
}
if (Test-Path ./$filename.exe)
{
Remove-Item ./$filename.exe
}
try
{
Write-Output "compile..."
cl /DNDEBUG /EHsc /MD /wd4355 -I "D:\Program Files\gecode\include" `
-c ./$filename.obj -Tp ./$filename.cpp
Write-Output "link..."
cl /DNDEBUG /EHsc /MD /wd4355 -I "D:\Program Files\gecode\include" `
-Fe ./$filename.obj /link /LIBPATH:"D:\Program Files\gecode\lib"
Write-Output "run..."
sudo ./$filename.exe
}
catch
{
# 目前 try 捕获不到编译器的异常
# issue: need help
Write-Warning "Error: $_"
exit
}
第一次写 ps 脚本,还望大佬多多指教。有几点需要注意:
如何使用?
当我有一个脚本 abc.cpp ,我只需要在命令行输入
test abc
就可以自动 编译->链接->运行C++文件abc.cpp
了!
编译前:
│ abc.cpp
│ test.ps1
编译后:
│ abc.cpp
│ abc.exe
│ abc.obj
│ test.ps1
[1]
gecode说明书MPG: https://github.com/Gecode/MPG
[2]
笔记: https://piperliu.blog.csdn.net/article/details/114858671