简体   繁体   中英

Running C++ programs in Sublime Text 3 MacOS

I went to a tech camp this summer and learned how to program using C++ in Visual Studio, but unfortunately, I did not have a Windows computer at home. I tried to get Visual Studio for Mac to work, but I was not successful and decided to try Sublime Text 3. I finally was able to get a simple "Hello World!" program to work that I had originally created in Visual Studio at my camp.

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World!" << endl;
}

But when I try to make a new program that does the same thing, instead of saying Hello World!, it says:

ld: can't open output file for writing: /Users/David/Desktop/test, 
errno=21 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see 
invocation)
[Finished in 0.4s with exit code 1]
[shell_cmd: g++ "/Users/David/Desktop/test.cpp" -o 
"/Users/David/Desktop/test" && "/Users/David/Desktop/test"]
[dir: /Users/David/Desktop]
[path: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands]

I looked at the folder with the "Hello World!" program that I transferred from Visual Studio, and it has another file in it other than the .cpp file that opens up terminal. First it shows 2 file paths followed by exit; and then it shows:

Hello World!
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

Is this the file that I need to get my new program to work? How do I get this file? Also, another problem I have been having with Sublime Text 3 is that I can't run programs that have multiple files in them. For example, this turn-based text game I made at camp will only print the first line if I run the main .cpp file in the folder. If I run the .sln file, it comes up with an error that says:

ld: warning: ignoring file /Users/David/Desktop/iD Tech C/David 
M/textTournament/textTournament.sln, file was built for unsupported 
file format ( 0xEF 0xBB 0xBF 0x0D 0x0A 0x4D 0x69 0x63 0x72 0x6F 
0x73 0x6F 0x66 0x74 0x20 0x56 ) which is not the architecture being 
linked (x86_64): /Users/David/Desktop/iD Tech C/David 
M/textTournament/textTournament.sln
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see 
invocation)
[Finished in 0.1s with exit code 1]
[shell_cmd: g++ "/Users/David/Desktop/iD Tech C/David 
M/textTournament/textTournament.sln" -o "/Users/David/Desktop/iD 
Tech C/David M/textTournament/textTournament" && 
"/Users/David/Desktop/iD Tech C/David 
M/textTournament/textTournament"]
[dir: /Users/David/Desktop/iD Tech C/David M/textTournament]
[path: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands]

Is this because when I run it (by pressing command+shift+b) I am selecting C++ Single File - Run? How can a run multiple files?

Here is the folder of the program:

https://docs.google.com/drawings/d/142bQ92Cr9lyS7ABap31ID-MkVn68IhdiyHXNfw_mDAk/view

(By the way, how do you insert an image into this?)

Start by downloading XCode - this will install whole development environment.

You can develop in XCode . If you prefer to stick to Sublime - that's fine.

Just create your code, save it inside hello.cc and make sure to choose C++ build system.

Tools -> Build System -> C++ Single File

在此处输入图片说明

Once you have your code in editor

#include <iostream>
using namespace std;

int main() {
    cout << "Hello world!" << endl;
}

在此处输入图片说明

make sure to choose: Tools -> Build Width ... and select Run

在此处输入图片说明

That's it.

在此处输入图片说明

Even though I am huge fan of Sublime Text (note missing UNREGISTERED message in top-right corner), I'd definitely suggest to use either CLion or XCode for heavy C++ development purposes. Otherwise, it will be hard to debug the code.

CLion: https://www.jetbrains.com/clion/

XCode: https://apps.apple.com/pl/app/xcode/id497799835?mt=12

Visual Studio Code: https://code.visualstudio.com

ld: can't open output file for writing: /Users/David/Desktop/test, errno=21 for architecture x86_64

Errno 21 is EISDIR (use man errno in the terminal to see more about what errno is and what the values mean), which means that the path it's talking about is a directory. Based on the paths involved, I would guess that your program is named test.cpp but there is also a folder named test on your desktop, so when the linker tries to create the final application, it can't because the file it's trying to create is a directory. Renaming your input file or removing/renaming the test folder should solve the problem if that's the case.

I looked at the folder with the "Hello World!" program that I transferred from Visual Studio, and it has another file in it other than the .cpp file that opens up terminal. First it shows 2 file paths followed by exit; and then it shows:

This looks like a log file that Visual Studio generates, possibly as a result of running the debugger. As such it lets you see what happened at some previous point when you were doing something, but it's not necessary for things to work; if you were using Visual Studio, it would recreate such a file as needed.

Also, another problem I have been having with Sublime Text 3 is that I can't run programs that have multiple files in them.

Indeed the build system for C/C++ that ships with Sublime presumes that you're only compiling or compiling and running a single file application, as the name suggests. It's meant for small, simple applications/tests, so anything that consists of more than one source file is out of scope.

That build system contains these two lines (the first is just for compiling, the second is for compiling and running):

"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""

There isn't any magic here; Sublime just executes an external program to do the job. In these lines, ${file} is substituted with the name of the file you're editing. Thus if you had two files, you would need to hard-code one or both file names in the command.

The result of that is that every time you add/remove a file from your project, need to change the compiler options, or switch to a different project, you would need to modify the sublime-build file to suit, which is somewhat suboptimal.

Additionally if your program contains say 10 source files, every time you change one it would have to recompile all 10 of them even if it doesn't need to, which wastes time.

In a case like this you want a dedicated Build Automation tool such as make (or something similar to it; there are many). Those tools do the work of knowing what needs to be compiled and how to create the required output. How to use make is outside the scope of a Stack Overflow answer but Sublime contains a build system for it out of the box, so if you follow a tutorial and create a Makefile , Sublime will find and it use it to build your program.

For example, this turn-based text game I made at camp will only print the first line if I run the main .cpp file in the folder.

This one is a bit unclear, but it sounds like your program will compile and run OK but seems to stop?

If that's the case, your problem is that you can't run an interactive program directly in Sublime (where interactive means a non-gui program that allows you to interact with it in any way, such as a game would) without using a third party package of some kind. When Sublime runs a program it captures stdout and stderr (or for you in C++, it captures what you send to cout and cerr ) but it doesn't send anything you type into the output panel to the application (ie cin will do nothing).

The result of that is that as soon as your program reaches the point where it asks you a question, it's now hung forever; it's waiting for you to tell it what to do, but you can't.

Using a third party package in Sublime such as Terminus lets you get around that, though it requires that you know how to manually compile and run your program in the terminal to be able to set it up.

If I run the .sln file, it comes up with an error that says:

A sln file is solution file; it's a file that is used by Visual Studio to help you manage your application. It stores things like the names of the files in the project, what the output should be named, and so on.

The gist of the error message is that it's trying to run it as if it's an executable, but it's not. Only Visual Studio cares about that file (as well as many others outlined in your image). Many of them are files that are intermediate files generated by Visual Studio itself for it's own use. When all else fails, worry only about the cpp and h files as the source of your program.

(By the way, how do you insert an image into this?)

There's a button in the toolbar above the edit area that allows you to insert an image.

As an aside, note that Sublime isn't an IDE in the same sense as Visual Studio or XCode; where those tools are purpose made for writing software and they provide options and guidance for setting up your project, Sublime is more general purpose and requires you to tell it what you want it to do.

This gives you a lot of power but also requires more from you to get it set up to begin with, which is a bit of an extra hurdle if you're just starting out.

{
  "cmd": ["bash", "-c", "/usr/bin/g++ '${file}' -std=c++11 -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
}

It's for Mac

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM