When I open a file with an ending of ".c" in Sublime Text 3, the program displays it in C++. I know this because the language shown in the bottom right hand corner is "C++". Can I change this?
I don't know if it is related, but when I try to run or build a C program, Sublime Text compiles it using g++ and not gcc. Also, I'm using a linux OS.
Thanks.
You can just click on that C++, a menu shall appear in which you can select C.
Since you're saying that all the files with .c extension are being viewed as C++ files, you may also want to change that default by navigating to:
View >> Syntax >> Open all with current extension as... >> C
While having a .c document as the active tab/group/window.
Sure you can,
in the tool bar select Tools->Build System->New Build System...
this will allow you to create your custom setting build.
After that you can remove Build System from Automatic and set it to your custom Build.
Hope it helps.
Related
i know a lot of people asked this question, but i can't find how to do it. Is there
a way to build only one source file in visual studio 2017? without new project, i'm learning c++, so i can't make huge thing now, just focus to code(now i'm learn data structure and algorithm),most of my exercise is about <200 code lines, so it great to compile new file without whole project, sometimes i need a few lines of code to test my algorithm,please help me, thanks all you guy, because v.s is very good ide so i want to stick with it.
If you just have one file and want to build it without waiting 1-2 minutes for the IDE to pop up,
Find the Developer Command Prompt in your list of applications - it is under the Visual Studio directory in the Application menu.
cd /d to your directory. cd will take you here if you are on the same drive as visual studio. If you are on a different drive, use cd /d.
Use your favourite editor (notepad, vim, geany, notepad++, nano, microemacs etc) to create the file.
cl sourcefile
Run the excutable.
Unlike what visual studio does, you executable will now be in the same directory as your source. Editors like geany have a build button (the brick icon). All you need to do is fill in how to build: in this case, the cl command.
If you want a one file project, just follow these steps.
Create New Project - File -> New -> Project
Fill in filename, select Win32 Console Application. Note the directory - if it is not where you want it, change it. Click OK
Application Wizard pops up, click Next
Application settings - select Empty project, click Finish
Open Solution Explorer. Right click Source Files. Menu pops up, select Add -> New Item
Add new item dialog pops up, fill in your filename.
If you don't know how to create a new project and a new solution, it will be good to learn those basic concepts and use them to write, test, and debug your code.
You can use one Visual Studio project to do all the learning.
Let's say you want to test "algorithm 1". Then,
Create a header file for it and a source file for it -- call them "test-algorithm-1.hpp" and "test-algorithm-1.cpp".
Add them to the project.
#include the header file in the main .cpp file of the project.
Call the function to test "algorithm 1" from main.
#include "test-algorithm-1.hpp"
int main()
{
test_algorithm_1();
}
When you are ready for testing "algorithm 2", repeat the above steps. The main .cpp file can now be.
#include "test-algorithm-1.hpp"
#include "test-algorithm-2.hpp"
int main()
{
test_algorithm_1();
test_algorithm_2();
}
If you want to avoid testing "algorithm 1" while testing "algorithm 2", simply comment out the corresponding line in main.
int main()
{
// test_algorithm_1();
test_algorithm_2();
}
On the source file you don't want to be included in the project, simply right click, select Properties. There you will find in General a field 'Excluded From Build'. Type true/yes there and the source file will be deactivated.
I'm debugging a program in Visual Studio and I'm wondering why my program passes next to a function in a particular unit (source code file).
I thought that the call stack would show this, but this seems not to be the case. Moreover the list of showable entries does not even mention the filename:
Show Module Names
Show Parameter Types
Show Parameter Names
Show Parameter Values
Show Line Numbers
Show Byte Offsets
=> no "Show Filenames", although the presence of "Show Line Numbers" means that the filename should be known.
I'm working with Visual Studio Professional 2017 for Windows Desktop.
Is this a configuration issue, is this not present in my Visual Studio installation (if so, which version has this feature) or is this not foreseen in Visual Studio at all?
For a good understanding, my call stack looks like the following:
<product>.dll!<first_class>::<first_class>(double dFy, double dFx) Line 102 C++
<product>.dll!<subfunction>(double dFv, double dFh) Line 1450 C++
<product>.dll!<subsubfunction>(double dFv, double dFh, int number) Line 1672 C++
...
While I'd like to see something like:
<product>.dll!<first_class>::<first_class>(double dFy, double dFx) File <file1.c> Line 102 C++
<product>.dll!<subfunction>(double dFv, double dFh) File <file2.c> Line 1450 C++
<product>.dll!<subsubfunction>(double dFv, double dFh, int number) File <file2.c> Line 1672 C++
...
As I'm particularly interested in the difference between central source files and project specific source files, I'd like to see the whole path in the filename.
You can stop looking, the option is simply not available.
Hard to guess why Microsoft decided to not provide it, but it is the only debug info that is ambiguous. Something that the softies need to deal with every day, the programs they debug are generally built on a build server from source code that is not located in the same directories.
I'd like to browse through the own files, without needing to open all included individual central files...
That is not necessary, simply double-click the specific entry in the stack trace that you are interested in. If the debugger knows the source file then it will automatically open and position the caret at the statement. Hovering the editor tab displays the path of the file. If the debugger can't find the source file then you'll get a dialog to navigate to the correct directory.
From what I know there is no such option (in any version of Visual Studio). Only thing you can do would be to right-click on call stack and press go to source file. Then you would see source-code and hopefully you will know which version of a file it is. As far as I know there are also no plugins for what you are trying to accomplish.
The "Copy With Source Paths" Context Menu option exists in VS17 (15.9.45), VS19 (16.11.11), and VS22. Unfortunately, there is no documented option in the command window.
So recently I started switching from sublime text 3 to Visual Studio Code, and there are a few things which bother me, so I was wondering if its possible to change/configure this:
When selecting "Open with Visual Studio Code" from the Windows Explorer on a file which is already opened, visual studio will still reopen the file in a new window.
For my C++ code, some inline method definitions are in files ending with .I (I am unable to change this). I already configured VS Code to detect those as C++ files, however Alt+O does not switch between the .I and .h / .cpp file. (It does fine between .h/.cpp)
Is there something like a hex view for binary files? Right now I am unable to edit them with VS Code.
I added some files to my exclude list in search.exclude and files.watcherExclude. Those are properly ignored in the quick-change bar, but they are still shown in the file-hierarchy on the left. Is it possible to hide them there as well? Found it: its files.exclude
When creating and saving a new file, the save dialog always starts at the project directory. Is it possible to change this, so that it defaults to the path of the last-opened file, just like in sublime text?
Is there an option/extension to have a code-minimap next to the scrollbar, like in sublime text?
Regarding 6.)
put something like this in your settings.json:
"editor.minimap.enabled": true,
"editor.minimap.maxColumn": 200,
"editor.minimap.renderCharacters": false
I've been using windows in a class I've been taking but I am trying to run a basic code to figure out how to open/close/input/output from files on Xcode and the code I usually use on visual studios isn't working any idea why? thanks!
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
string input;
fin.open("inputFile.txt");
if(fin.fail())
cout << "File failed to open." << endl;
fin >> input;
fout.open("outputFile.txt");
fout << input;
}
Put your .txt files in the same directory where your main.cpp file is (or anywhere you like).
In Xcode go to Product > Scheme > Edit Scheme > Run (on the left) > Options (middle top)
Down under Options for "Working Directory" check “Use custom working directory” and set it to the directory where you .txt files are located.
To work with the files, you will have to specify just file names, e.g. in_file.open("inputFile.txt"); no path is necessary.
Here's a completely different approach: Have Xcode copy the input file for you.
Select your project in Xcode
Select Build Phases
Click the '+' button to create a new Build Phase
Select New Copy Files Build Phase
Select Products Directory
Click the '+' button to add your file
Click Add Other
Select your input file and click Open
Check the Copy items… checkbox and click Finish
Now every time you build your project, the input file will be copied to the same folder as the executable no matter where it is built. Of course, to see the output file, you'll still need to find the executable in Finder.
The answers don't really explain the problem so I thought I'd do that.
When you pass a relative path like "inputFile.txt" to file APIs, it's treated as relative to the working directory when the program is executed. This is the same as the 'working directory' when you use cmd.exe or Terminal.app or command lines in general. The Unix command pwd ("print working directory") displays the current working directory. On Windows running the command cd with no arguments performs the same function. (On Unix running cd with no arguments will change the working directory to the user's home directory.)
When you run a program from the command line, the command line shell sets the program's working directory. When you run a program from within an IDE, the IDE sets the working directory. Since, unlike on a command line, there's no obvious answer for what the IDE should set as the working directory, Visual Studio and Xcode set the working directory to different locations by default: Visual Studio sets the working directory to $(ProjectDir), the directory containing the Visual Studio project file; Xcode sets the working directory to the build products directory, i.e. the location the executable was written to.
Some possible solutions to your problem are:
Do not use a relative path, and therefore don't depend on the working directory. This isn't much help in making the program more portable, because the absolute paths will also differ between platforms, and so you will still have to 'configure' the program for each platform. In fact using an absolute path is worse, because it means your source code must differ, whereas it would be better to keep that difference confined to each platform's build configuration.
Configure the IDE to use your desired working directory. Visual Studio can be configured by right clicking the project, selecting Configuration Properties > Debugging > Working Directory, and setting the working directory to the desired path (potentially using Visual Studio build variables).
nepete's answer describes how to configure the working directly set by Xcode.
Configure the IDE's build process to copy your data files to an appropriate location. In Visual Studio you would do this in a C++ project by configuring the project's Properties > Configuration Properties > Build Events.
SSteve's answer covers how to configure additional build steps in Xcode.
I'm guessing you have inputFile.txt in the folder that contains your source code. That's not going to work. You need to put it in the folder that contains the generated executable. To find that folder, right-click on your app under Products and select Show In Finder.
This image shows what it looks like for a command line program. It also shows the Finder window that was opened. As you can see, it is a different folder than the one containing the source code.
As suggested by nepete, edit the scheme, but use $PROJECT_DIR as the custom working directory. Helps with moving the project around, or working in two different environments (e.g., home and office).
BTW. $PROJECT_DIR is one of the Xcode Environment Variables, and also helps with passing file names as command line arguments to programs (settable under "Arguments" in the scheme).
I've struggled with the same problem today. I wanted to add C code to my Swift project and my file pointer was always NULL.
Unfortunately, in XCode 9 for iOS app, I couldn't change the working directory. Changing Build phases didn't help me either. After 4+ hours of trial and error, that's what I've come up with finally and it works:
when copying files to XCode, I've chosen "Create groups", but I needed to choose "Create folder references":
I created a new objective-c file (.m) and copied all my C code there.
I left untouched .h files (XCode generated bridging header and my own .h file with public functions declaration). Now my project structure looked like this:
In my dict.m file in place of previous plain c fopen part:
FILE *dic = fopen("dictionary.txt", "r");
I added obj-C code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"dictionary" ofType:#"txt"];
FILE *dic = fopen([filePath cStringUsingEncoding: NSUTF8StringEncoding], "r");
And it works now without any problem! It's just amazing!
ps I decided to write this answer in case it will help someone like me and will save them some time. If you know how to change working directory in XCode 9 for iOS, please, leave me a comment - now I am really curious why I can't find it.
I'm trying to write a few algorithm programs practising for Google Code Jam but I can't get XCode to read from an input file (easily).
I have added the file to the project and it appears in the left hand file navigation bar. But I can't reference it relatively. In Code::Blocks I just input from "FileName.txt" and it works but with XCode it just can't find it.
If I put the full file path (i.e. /Users/myname/Documents/Google Code Jam/ProjectName/ProjectName/FileName.txt) then it works but that just seems ridiculous.
Is there anything I can do to get XCode to work? When I added the file I ticked the copy file into project directory and also the add to target executable.
Any help is appreciated.
Thanks!
I just found this and the comments by Julio Gorgé is just what I needed. The default working directory when running in debug mode is some esoteric folder (as you can see in iOS if you include system("pwd"); at the beginning of your main().
It's in the "Options" tab of the Edit Scheme... menu that you can tick "Use custom working directory" and find the test file you put in in the Arguments tab.