compile single c++ source file in 1 project in visual studio - c++

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.

Related

My Visual Studio can't find files and so can't do basic operations. What's wrong with it?

Using C++, if I launch an 'empty project', create a new C++ file, and try to run it, I just get this error message:
Unable to run program 'C:\Users\User\source\repos\Project2\Debug\Project2.exe' The system cannot find the file specified
I go to the file it's referencing and the file IS there -- what??
Using a 'Console App' project is different: it will actually compile and run the code.
Similarly, I can't join new header files to the main file, not even in a 'Console App' project: if I write the code for doing #include "Header.h", I get a red line underneath #include, and if I hover over that it says:
cannot open source file "Header.h"
I'm new to coding, and don't know why I'm having such a seemingly absurd problem here. Help!
You should create a new project and then create a c++ file. While adding a file, make sure to select c++ console application and do not check empty project if you are new.
From the result you described, I suspect you used File -> New file to add that cpp file, right? This does NOT add that file to a project.
Instead, you should right-click the Project file in the Solution explorer, select Add new item (or Project -> Add new item menu), and choose the C++ file type.
This works!

Source file header file and other files are not displayed when I create empty C++ Project in visual studio 2019

I am very new to visual studio and I watched a couple of tutorials to understand how to use it, But I ran into a problem. Every person I watched on YT has a source file, header file, and other files when creating an empty Project but this isn't a case for me and I basically can't write anything.
when I try to add a CPP file it doesn't even show an option to add it, it only shows class and resource when I right click and press on add. Basically, I want the files to show so I can create my main program and play around with it.
Help is much appreciated.
First Create an empty project .
Second Go to solution explorer.
Third Right click the source file option.
Fourth Go to add and create a new item
Fifth Select .cpp file and press add.
And Congrats your .cpp file is created. Do the same procedure to create header file and class.

Visual Studio - C++ New File Default Code

Hey everyone I have a comfort problem is Visual Studio 2017 (I use 2017 because of school requirement).
Always when I start a new C++ Project or C++ File I get the default code:
// ConsoleApplication4.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
It's very annoying for me to delete it every time and I'll be happy to know how to remove the default code. I searched the Internet and Microsoft MSDN and I didn't find any solution. Thank you for your help, this website helps me a lot!
There are two methods to create a empty project.
1.1 Create a project.
1.2 Edit the project until it is ready to be exported as a template. For example, you might want to edit code files to indicate where parameter replacement should take place. See How to: Substitute parameters in a template.
1.3 On the Project menu, choose Export Template.
The Export Template Wizard opens.
1.4 On the Choose Template Type page, select Project Template. Select the project you want to export to a template, and then choose Next.
1.5 On the Select Template Options page, enter a name and optional description, icon, and preview image for your template. These items will appear in the dialog box where you create a new project. Choose Finish.
You could use Windows Desktop Wizard.
Then select Empty Project.

New to Xcode can't open files in c++?

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.

Creating shader files

I'm trying to learn some DirectX11 and I found good tutorial I'm following; however, for some reason the program crashes (The window appears but then a "not responding" message box appears), even though the build was successful. There were some thing about creating shader-files the author did specify on how to do, and i wanted to make sure I'm not making any errors there and that why my program crashes. So my questions are :
How do you create a shader files? In the tutorial we are using the extension .fx, but when you add a file in vs you can only choose from .h and .cpp . Do you only have to select .cpp and then add .fx in the end of the file name?
In what directory is the file suppose to be? (My files are currently in Source Files)
Yes that will work. Another way to do it is just to create a .txt then change the extention to .fx
(To show the file extention just open any folder, click on the organize button in the menu then choose "folder and search options". Uncheck the box "Hide extentions for know file types" under the tab view). After you change the extention you can open the document in notepad and write your code there
Go in to your project folder (in windows file system) and create a map called Data. Add you .fx file there. Then go to your solution explorer and rigth click on the project and choose properties. Go to Build Events -> Pre- Build Events. Add the line "xcopy /y /d "$(ProjectDir)Data" "$(OutDir)" in the box "Command Line". Now when building your project the files will be copied to the rigth place and you will be able to use your .fx file.
The .fx is associate it with an effect file. Meaning you have a combination of many shaders in the same file. Can you post some code of your render loop... also, are you calling the Present method?i.e.,
m_pSwapChain->Present(0, 0);