Xcode, how to run one file at a time? - c

Xcode Question, how to run one file at a time?
I keep getting this error: "Linker command failed with exit code 1" and I've noticed that it occurs when i use the "int main" method in multiple files. Furthermore, when I try to run a simple return 0 file, my "hello world" file still gives results to the console. So my assumption is that xcode is running all of the files in the project at the same time causing me to have a duplication error when I repeat method names (such as "int main"). How do I only run one file at a time? These are C files by the way, I'm using xcode as a tool to practice my c programming before next semester. Thanks for your help!

It sounds like you're trying to build multiple programs. The problem is you have a single "target" which is what Xcode uses to define what goes into an application.
You will need to create a separate target for each of your programs and assign target membership for your source files. Go to File->New->Target to create a new target. From the sounds of it, you are creating a command-line C program, so you will want to create a Command-Line Tool found under OS X-> Application.
Alternatively, you could also create separate projects for each program. See File->New->Program
As yet another alternative, assuming you are creating command-linen tools, if you wish you can use Xcode merely as an editor and build the program from the commandline (which you may have to do for your classes anyway). You can do this by creating your .c files and opening them in Xcode. Save the files to the same folder. To compile from the commandline, run something like the following in Terminal:
gcc -Wall file1.c file2.c -o myprogram
You would then run your program by giving:
./myprogram
If that doesn't work, make sure the command line tools are installed.

You can only have one int main per target.
You're getting linker errors because you're defining more than one.
If you need to run a function other than main at the start of execution, call it from main.

I ran into this issue myself following Learning C by Dan Gookin with exercise files. I learned that in order to be able to run the .c files, a project must be created then the files must be added specifying no target membership, otherwise Xcode will compile the entire files three as one executable and the errors encounter would prevent the program to run.

Related

How to Use make to compile c file on codespace

make CLI keyword is failing to compile my c programming file.
There seems to be 2 different issues there:
1- make seems to not be present in your workspace. Please try running rebuild50 to rebuild it. You'll not lose your files.
2- You're issueing the command from outside the correct folder.
Please note that in hello folder there are two distinct files (hello and hello.c), but the output from your ls command shows only one result (which is the folder, not the files).
Please run cd hello first to get into the folder containing the file to make.

Equivalent of 'make' command for CodeBlocks?

I am using Codeblocks in Windows 10. Earlier I used to compile only 1 file, so I would just press the 'Build' and 'Run' button on codeblocks.
But, this time there are separate files I need to compile separately (to .o format) and then link to form a single executable file which I need to run. How can I do this in CODEBLOCKS? (In UNIX/LINUX systems, I know this is the 'make' command; but I don't know how to do it here?)
Can anyone help me? I tried reading some help pages on CodeBlocks on the internet, but they were so complicated, I gave up.
make isn't a compiler, and neither is CodeBlocks. make is just a command that runs other commands from a Makefile. The actual command that gets run is something along the lines of gcc a.cpp b.cpp, and it's the same command (or similar enough) that gets run when you build inside CodeBlocks. CodeBlocks should automatically build and link all of the source files that you add to your project. If you aren't seeing this happening, make sure you add them to the project inside of CodeBlocks and don't just put the files in the directory.

How to setup eclipse with C for university work

I'm fairly new to eclipse but have figured out how to use it with Java.
However, we are now moving on to C and I am having a hard time using it. I just want to use eclipse for my labs - i.e. to create / compile / test / run C exercises or tasks that have been set.
I created a new 'Labs' C project and have been creating the files ex1.c, ex2.c etc in the src folder. Eclipse doesn't like this due to more than one main across multiple files, but the files aren't related and each is supposed to have their own main.
Can someone advise me as to whether there is a better way to setup / organise my workspace for this labwork or alternatively how to compile / run single files at a time in eclipse?
You have a few options:
Create one project per executable (the projects can be in the same workspace). This is pretty self-explanatory, but it might get annoying if you have a lot of executables. Also, if you need to share code between your executables, you'd have to create a separate project for the shared code and set up dependencies.
Create one project with multiple build configurations, one per executable. See this answer for how to do that.
Use Eclipse for navigating and editing your code only, and build (and run/debug) your executables from the command line. This way, the organization of the Eclipse project(s) is irrelevant, and you can build from the command line however you want (at this stage, a simple Makefile is probably the easiest).
I prefer option #3, but to some extent it's a matter of taste; if you like to do everything including building from the IDE, go with #1 or #2.
EDIT: A simple Makefile might look like this:
ex1 : src/ex1.c
gcc -o ex1 src/ex1.c
ex2 : src/ex2.c
gcc -o ex2 src/ex2.c
...
put into a file named makefile, and you would then run make to build. (If you're on Windows you would write ex1.exe instead of just ex1.)
Have a look at a tutorial like this one to understand how Makefiles work.

.exe file is not being created in windows for C by cmd by gcc compiler

i am also worried about how to make u guys understand about my problem and share my experience. i am learning C.in previous, i used the Code-blocks IDE to compile and run C code.i learned that as a beginner it would better to use cmd to compile and run code than IDE. i am using windows 8. Before i was not use to with cmd, i also learned that to be a advance user and good programer i should know the use of cmd well.
After setting the environment variable started to compile c code by cmd by the following command
gcc -o "executable file name" "C source code file name"
it craete a .exe file. but no .o file. when i worked with IDE, also a .o file was found with .exe
what is the possible reason for it?
after compiling the code, i ran it by calling "executable file name".
and when checked the directory, then a .exe file was found.
but now the problem is, recently i am compiling and running code, i found that there is no .exe file created, but a file has created with unknown extension"POINTERWHILE File (.pointerWhile)"(copied from file properties). this unknown extension file is only runing by the cmd. it may be the one part of my source code file name. the code has given below and the name of source code.
Name of source code: Example184.1.pointerWhile
#include<stdio.h>
main()
{
char *p = "stop";
char str[80];
do
{
printf("Enter a String: ");
gets(str);
}
while(strcmp(p,str));
}
this program will take input until, "stop" entered.
this kind of things happened for several other files.then i copy them, to an another folder and compile and run but the same result.
i complied and run the same code form same folder and other folder by codeblocks, the result is same. the file is only running when it is in IDE.
then make an another file with other name in same directory and other, but the same code. then it is creating .exe file.
2.what may the cause for creating .exe file?
Great effort from you in learning the cmd along with C!
However, I think you're confusing the extension (name) of the file, and its contents. When you tell gcc to generate an executable file, you might want to give it a .exe extension, just like you said, assuming your source is called "myprog.c":
gcc -o myprog.exe myprog.c
This should always generate a myprog.exe file. But nothing prevents you from calling it like this:
gcc -o myprog.myFancyExtension myprog.c
You should still be able to run the program via the cmd (I didn't test that on windows though). Note that on other systems, like Linux, executable files usually have no extension at all.
Also, you're talking about how your IDE generates a myprog.o object file along with the myprog.exe executable file. To do this, your IDE is calling the compiler (gcc) twice: once to generate the .o file, and another time to generate the .exe file. There is no way to generate both files in one run of the compiler. The advantage of learning the cmd is that you will learn what your IDE is actually doing for you without telling you. The drawback is that you have to actually learn it ;)
Usually, people do not like to use gcc by hand, because as your projects grow, they will use different files and it becomes tedious to recompile them all manually. To overcome this, you will want to use a build tool, such as Make. I'm not familiar with development under Windows, so I can't recommend any specific tool. The point of those tools is that when your project has multiple source files, you only have to call the builder once, like this for Make:
make
instead of manually calling:
gcc -c myFeature1.c
gcc -c myOtherFeature.c
gcc -c myYetAnotherFeature.c
gcc -c myMain.c
gcc -o myMain.exe MyMain.o myFeature1.o myOtherFeature.o myYetAnotherFeature.o
Hope it helps

About linking files using Mac terminal

Okay here goes, I'm completely new at this, started learning the terminal just about 2 days ago. I'm slowly but surely getting the hang of it, now I'm stuck on this and I've been trying to fix it for a good hour. It's a rather simple question as I am a newby.
I have a C file in my desktop and a Header file in a folder in my desktop. I'm including that header in my C file. I have to link them (currently doing a tutorial, it tells me to link, but doesn't show me how).
You have a couple of options. First, you will need to install the software development environment - it's called Xcode. I think you can get it for free on the AppStore, if not Google it.
Then you need to decide if you want to develop and compile graphically in the Xcode Integrated Development Environment. If you do, start Xcode and create a new project and open your C file and change the "include path" to match the location of your header file. Then click "Build" and "Run"
If you want to do things at the commandline, you'll need to install "Xcode Command Line Tools" - Google it. That will give you a compiler. Then you can compile. I'm not certain which compiler you will get - it could be "llvm" or "gcc" or something else, but the command you are looking for will be something like:
gcc -o prog -I /path/to/HeaderFileFolder yoursourcecode.c
which will give you a program called "prog" that you can run by typing
./prog
You are likely confusing two different concepts. The "link" mentioned in the tutorial is probably talking about turning the compiled objects into a single executable. See http://www.cprogramming.com/compilingandlinking.html for an explanation of what linking means in this context.
What you've provided examples of doing is file system linking, which is totally unrelated.
Providing more details on the tutorial could help refine this answer.

Resources