Unable to launch program from the IDE - c++

I have learned the basics of C++ but I have never used visual studio.
I would like to know why I get popup window that says "Unable to start program" and then lists a file path C:\folder\folder\folder\../../lib/Win32DB/ProjectNameDB.lib. (The message doesn't give me any more info, like 'the system cannot find the file specified' or anything like that.)
ProjectNameDB.lib exists, but not at that particular location. The project builds successfully, and the same path as above appears in the output after TargetPath =.
I have tried setting the project as startup, deleting .suo files and vcproj.user files, starting without debugging, and putting the location of ProjectName.lib in the Output, Library, Include, Reference Directories.

You can not start one *.lib but one *.exe. So build one EXE program you should use the below steps with Visual Statio 2013:
start vs2013;
choose File -> New -> Project;
choose Win32 Console Application, and write your project name, click OK;
click Next, click Finish;
Now, you can write "Hello World" in 'x.cpp'(here 'x' is your project name); the following code:
int main(int argc, _TCHAR* argv[])
{
printf("Hello Wrold!\n");
return 0;
}
save, build and start run it, it will print 'Hello World' in console.
exe file must have main function, but lib file is not necessary.

So... the problem was that the project was configured to run as a static library and not as an executable.
Properties -> Configuration Properties -> General

Related

Visual Studio. Unable to start program '' The system cannot find the file specified

Its my first program on Visual Studio on C#.
I did not write my own code and Im trying to open any template projects with simple console writes.
// ConsoleApplication1.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
However Im receiving error that I do not know how to solve.
Errors:
Unable to build properly.
When I try to build, there are tons of errors
eg
cannot open source file ... (tons of this)
the global scope has no .... (tons of this)
And when I try to run, obviously it wont run. It will tell me
"Unable to start program. 'C:\repo....\Debug\ConsoleApplication1.exe' The system cannot find the file specified."
This is my very first program and Im unable to run it. I believe its probably due to file path of the project - unable to find the appropriate files. I have no idea which setting to tweak as well... Probably a very simple solution but I cant find it online. Thanks.
According to your example code, this is a C ++ project and not a C # project. If you want to create a C# project ,you need different code.
If you created a c++ project, and got this error. I sugges you could follow the following steps to resolve the issue:
1,You should build the project before running it.
The cause of this error is most likely because you haven't built your project from Build > Build Solution. The reason you're getting this error when you try to run your project is because Visual Studio can't find the executable file that should be produced when you build your project.
2,Check if the linker output file matches the path, name and extension of the target file.
Right Click Project Name > Properties -> Linker > General -> Output File > $(OutDir)$(TargetName)$(TargetExt)

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

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.

Strange (c++) debug issues using VS2013

I have inherited a project to work on and the initial build was developed on linux. I dont know if this matters or not but thought I would share it.
In order to debug the project on a windows machine I first use the CMakeGUI on win7 to create a Visual Studio Solutions file to open the project using Visual Studio 2013 and then set the startup project and build the project I am interested in. Up till now everything is okay. Now comes the confusing part.
On load the program is suppose to read a file lets call it in.dat and is declared in const char * inputFileName this variable is then passed through a class which attempts to open then file to obtain data.
fstream fs;
fs.open(inputFileName.c_str(), fstream::in);
if(!fs.is_open())
{
std::cout << "Cannot open input file!" << std::endl;
exit(0);
}
This where I am stumped...the file when placed in the debug folder for some reason cannot be opened i.e fs.is_open() returns false when I try debugging the application BUT if I cd directly into the debug folder of the project, outside of VS, and run the executable it runs as expected i.e fs.is_open() now returns true.
Your debugger's working directory defaults to your project's root directory and the binary is in the \debug subdirectory, resulting, in effect, to the path to the input file being wrong.
Set the debugger's working directory to \debug.
Here is more info on that:
https://msdn.microsoft.com/en-us/library/kcw4dzyf(v=vs.120).aspx

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.

"launching 'project' has encountered .." , project file does not exist

I wanted to write 'hello world' in eclipse c++, but it does not work
I go to Run configurations, what config options for c++ programs should i give?
I know I don't care about 'debug' - only 'release', but how to do that?
Here is what I did:
File->New->C++ Project
You will get a pop up window. Type the name of the project you want. Then, below it says executable and inside this folder, I have (by default I guess) Empty Project.
Then click Next and Finish.
Now the project appears in the left column of Eclipse. I right click it and select New->File and name it main.cpp
The main window of Eclipse opens the file main.cpp and I write inside:
#include <iostream>
int main() {
std::cout << "Hello Erjan\n" << std::endl;
return 0;
}
Then I click on Build, it's the hammer icon in the middle of the toolbar. The code compiles and we are ready to launch it!
So, click on Run icon (3 positions right of the Build icon) and you should see the output in the console.
The first step, create new project and try proper toolchains.
Second step, Project >> build project.
Third step, right click on new executive file and run or debug it.