Add multiple independent main() programs in one project - XCode - c++

I've very big problems with XCode. First of all, I want to tell you that my starting language was Java. In Java you can write multiple programs in one project, which is very useful for smaller programs, f.e. Hello World.
Now I want to learn C++ and write some programs for exercise. I already wrote one in my project "Uebung" (= Exercise). Now I want to write another program, which is completely independent of my previous program, but it's still an exercise program, so I want it in my "Uebung" project.
I've researched and found out that targets are my solution. Unfortunately, I can't figure it out how to configure them properly.
You can see the build Phase of my program "NumberCounter". Look at the bottom right corner, there you'll see that it works:
Now I want in my Project "Uebung" and in the Folder "Uebung" a new program with a main.
I go to File -> New -> Target -> Command Line Tool and choose a name for my new target.
Now a new folder pops out with the same name as my target. Note that the target is red and I don't know why. The Compile Source is the new main in the folder "test":
Now I want to compile "Test", it should just print "Hello, World!", but it doesn't as you can see. It compiles my "NumberCounter" program:
So I thought, maybe my order was incorrect. I delete my new target and the folder "test" with the main in it. Now it looks exactly like in the beginning.
I create a new C++ File "test" and now I can choose a target. I'll uncheck my "Uebung" target, because this isn't the one I want:
Now It looks like this and this is exactly how I want it:
Same Procedure: I go to File -> New -> Target -> Command Line Tool and choose a name.
Again, a new folder pops out but this time it is red.
This is how my build phase looks like:
This isn't how I intend it to be so I change it to this:
Since I don't need the "test" folder I simply delete it.
I wrote down some code in my new program and compile it, but again it runs the wrong program:
.
I hope there's somebody who can help me.
Thank you very much!

Everything you did the first time to add the 2nd target test was correct.
Your issue is that once you have two or more targets, you need to choose which target is the active target. Look at the titlebar at the top of the Xcode window. See the Play and Stop icons? To the right of those it says "Uebung" followed by "My Mac". Click on "Uebung" and select "test".
Now your "test" target is active. You can now build and run that target.
FYI - "test" appears red under Products because you have not yet built that target.

Related

Xcode writing to file issue

I'm trying to help my son with a Programming C++ for Engineers course. I got him set up with Xcode 13.1 on his iMac but I've never used it myself. That was working great until his first assignment that required writing output to a file. We created a new Command Line Tool project and then used the File->New->File... option to create an Empty file. After doing so, selecting that file and looking at the info on the right side of the window shows that the Full Path is pointing to the directory where his main.cpp file is. Yet, when running his code nothing was being written to the file. I had assumed that just referencing the file name in his code would cause it to use a local reference and look in the same directory as main.cpp. After much Googling I found a reference that said to go to Product->Scheme->Edit Scheme... then select the Options tab in the window that opens and change the Working Directory from "$(BUILT_PRODUCTS_DIR)" to the directory containing the main.cpp file. That got the programming working correctly. My questions are:
Is there a preference setting that would tell it to always use the current project directory so that we don't have to change this every time?
Where does "$(BUILT_PRODUCTS_DIR)" point to?
What should we be doing differently?

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.

Where to start for writing a shell script for copying elements into main app xcode4

I am looking for some documentation or tutorial for copying files from a given directory into the app created by xcode at build time, before it is run.
At first I have tried to copy files into the derived directory, hoping that everything resides in there would be automatically added to the app, but I was wrong.
So I am looking for a script because the original dir may change its name, second the script could be customized by another xcode 4 user with its src dir path etc.
The things is I don't know how to start, which language etc. I am quite confident with shell script, but maybe there's a better option.
Second, I am trying to figure out which command could add a file in the already built app.
thanks
That answer didn't really help - the BUILT_PRODUCT_DIR isn't where most stuff goes.
Ultimately, I found you just need to do:
Add the following to the very end of your script (or get your script to write directly to the output location):
cp ${DERIVED_FILE_DIR}/[YOUR OUTPUT FILES] ${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}
...but there's a lot of other things I tried. More thoughts and ideas here: http://red-glasses.com/index.php/tutorials/xcode4-a-script-that-creates-adds-files-to-your-project/
You want a Run Script or Copy Files build phase. Select your main project in the navigator, then select the app's target. Click the Build Phases tab. Click the Add Build Phase button at the bottom of the window and choose the appropriate phase.
By "appropriate" I mean if you really want to run a script, you'll use a Run Script build phase and use Xcode-provided environment variables like $BUILT_PRODUCT_DIR (see the documentation or hit build and examine the full output of an empty script in the build log) to figure out your target folder. If all you want to do is copy files (no real processing), the Copy Files build phase already knows how to locate the app bundle's proper folders depending on what you're copying (Resources, Frameworks, etc.).

Changing the main file in a C++ netbeans project

When I create a new C++ project in netbeans,it prompts me to choose a name for the main file of the project
this is the file that gets executed when I press "Run" in the IDE
does anybody know how to change this file to another one AFTER I've already created the project?
You don't need to change the name or point to a different file.
In C++, on NetBeans or any other IDE, just place main function on a different file (the file you want to RUN when you press RUN on the IDE) and you should be done.
When we first time build and run a c project in netbeans- if there are more than one file with main() then it prompts us to select one of those files to be run. But after that it doesn't ask us to select a main file rather it uses the same file selected earlier, every time we run the program. So how to select other main file once we have selected one file during first attempt to run. I believe this is your question.
Answer: Right click project => select Properties => select make option under Build (in categories) =>Against Build result specify your file that you want to run. You can easily browse files with main() there. See picture-

kdevelop no valid executable specified

I've just installed kdevelop 4.1 , then created a normal hello world , build is ok but when i press execute it gives me (no valid executable specified) any idea ?
Meh, the launch configuration - our biggest usability problem...
Go to Run -> Configure Launches
select your project to the left
hit the "+" button
to the right, select your project target
optionally add a build dependency (again, select e.g. your target).
that should be it.
/me really wants to rewrite this dialog...
After following the steps provided by milianw. In case it doesn't work, follow this steps:
Go to Run >> Current Launch Configuration
choose the current file name you are trying to compile.