TrampolineTales
TrampolineTales

Reputation: 826

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

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):

$ 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:

[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?

Upvotes: 0

Views: 467

Answers (1)

melak47
melak47

Reputation: 4850

The cmd key in .sublime-build files specifies the program to run. 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.

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):

{
    "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.

Alternatively, you could also try using the env key (also found here) to specify the LD_LIBRARY_PATH environment variable before running your executable:

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

Upvotes: 4

Related Questions