简体   繁体   English

使用sublime-build时无法运行C ++程序,从终端运行时可以正常工作

[英]Cannot run C++ program when using sublime-build, works fine when running from terminal

I'm trying to run a C++ program on Linux/Ubuntu 14.04 我正在尝试在Linux / Ubuntu 14.04上运行C ++程序

I'm using the SFML library, the tutorial suggests to include this line before running if SFML was installed in a non-standard path (which it was): 我正在使用SFML库,如果SFML安装在非标准路径中(本来是),则该教程建议在运行之前包括以下行:

$ export LD_LIBRARY_PATH=<sfml-install-path>/lib

I can run the program from the terminal using the following input: 我可以使用以下输入从终端运行程序:

$ export LD_LIBRARY_PATH=/home/dan/SFML-2.3.1/lib && ./YorickTheSavant

However, when attempting to launch the program in Sublime Text 2 with a sublime-build file, I get the following error: 但是,当尝试使用sublime-build文件在Sublime Text 2中启动程序时,出现以下错误:

[Errno 2] No such file or directory
[cmd:  [u'export', u'LD_LIBRARY_PATH=/home/dan/SFML-2.3.1/lib', u'&&', u'./YorickTheSavant']]
[dir:  /home/dan/yorickthesavant]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/dan/.rvm/bin]
[Finished]

Here is my sublime-build file for reference: 这是我的崇高构建文件供参考:

{
"cmd": ["g++", "-m32", "-c", "-std=c++11",

"src/buff.cpp",
"src/card.cpp",
"src/clickableObject.cpp",
"src/creature.cpp",
"src/dataHandler.cpp",
"src/dungeonRun.cpp",
"src/enemy.cpp",
"src/gameQueue.cpp",
"src/gameSystem.cpp",
"src/graphics.cpp",
"src/hoverText.cpp",
"src/infoText.cpp",
"src/main.cpp",
"src/player.cpp",
"src/queueBlock.cpp",
"src/queueEffect.cpp",
"src/roundedCornerRect.cpp",
"src/save.cpp",
"src/ttText.cpp",

"-I", "include"],

"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "/home/dan/yorickthesavant/",
"selector": "source.c, source.c++",

"variants":
[
    {
        "name": "Link",

        "cmd": ["g++", "-m32",

        "buff.o",
        "card.o",
        "clickableObject.o",
        "creature.o",
        "dataHandler.o",
        "dungeonRun.o",
        "enemy.o",
        "gameQueue.o",
        "gameSystem.o",
        "graphics.o",
        "hoverText.o",
        "infoText.o",
        "main.o",
        "player.o",
        "queueBlock.o",
        "queueEffect.o",
        "roundedCornerRect.o",
        "save.o",
        "ttText.o",

        "-o", "YorickTheSavant",
        "-L", "/home/dan/SFML-2.3.1/lib",
        "-lsfml-graphics",
        "-lsfml-window",
        "-lsfml-system"]
    },
    {
        "name": "Run",
        "cmd": ["export", "LD_LIBRARY_PATH=/home/dan/SFML-2.3.1/lib", "&&", "./YorickTheSavant"]
    }
]
}

The "Run" variant is the one I'm using. 我正在使用“运行”变体。 How would I make it so that this line is automatically included when the game is launched outside of the terminal and in Sublime Text 2? 我如何做到这一点,以便在终端之外启动游戏并在Sublime Text 2中自动添加该行?

The cmd key in .sublime-build files specifies the program to run. .sublime-build文件中的cmd密钥指定要运行的程序。 The first value is expected to be the program and the rest are the arguments, so it fails when it cannot find a program called export - because it is a shell built-in. 第一个值应该是程序,其余的是参数,因此当它找不到名为export的程序时会失败-因为它是内置的shell。

To run the entire command in a shell (like you would when doing it manually), you can specify "shell" : true in your Run variant (or use shell_cmd instead of cmd ): 要在shell中运行整个命令(就像手动执行操作一样),可以在Run变体中指定"shell" : true (或使用shell_cmd代替cmd ):

{
    "name": "Run",
    "cmd": ["export", "LD_LIBRARY_PATH=/home/dan/SFML-2.3.1/lib", "&&", "./YorickTheSavant"],
    "shell" : true
}

This should work better than invoking /bin/sh -c <stuff> directly, and I don't think you'll need to condense the entries in the cmd array into a single string anymore. 这应该比直接调用/bin/sh -c <stuff>更好,并且我认为您不再需要将cmd数组中的条目压缩为单个字符串。

Alternatively, you could also try using the env key (also found here ) to specify the LD_LIBRARY_PATH environment variable before running your executable: 或者,您也可以尝试在运行可执行文件之前使用env键(也在此处找到)指定LD_LIBRARY_PATH环境变量:

{
    "name": "Run",
    "cmd": ["./YorickTheSavant"],
    "env" : { "LD_LIBRARY_PATH" : "/home/dan/SFML-2.3.1/lib" }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++代码仅在从终端运行程序时执行 - C++ Code Executes Only When the Program Is Run From The Terminal 确认Windows上.sublime-build正在查看的位置(Sublime Text 3,C ++) - Confirm location that .sublime-build is looking at (Sublime Text 3, C++) on Windows 将文件夹指针添加到默认的.sublime-build文件(Sublime Text 3,C ++) - Adding folder pointers to default .sublime-build files (Sublime Text 3, c++) 从Eclipse IDE运行可以正常运行,但是从终端运行时终止 - Run from eclipse IDE works fine but terminates when run from terminal 使用Unix终端运行C ++程序 - Run c++ program using unix terminal “运行C ++程序时,系统无法找到指定的文件” - “The system cannot find the file specified” when running C++ program 从WebAPI项目运行静态类时出现StackOverflow异常-从控制台应用程序运行时运行正常 - StackOverflow exception when running static class from a WebAPI project - works fine when run from a console application 奇怪的C ++错误,添加2条打印语句后程序运行正常,没有它们的段错误 - Weird C++ bug, program works fine when 2 print statements are added, segfaults without them 将代码链接到exe时C ++程序崩溃,但是将代码编译到exe时工作正常,为什么呢? - C++ program crashes when code is linked to exe but works fine when code is compiled into exe, how come? 在终端运行 C++ 程序 - Run C++ program in Terminal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM