So first I made a new project by going to File> OS X > Command Line Tool and chose C as the language. This is what I got:
Then I made a new C file called program.c:
Then I pressed run. It didn't build successfully:
However, when I deleted the first default file (main.c) and then pressed run program1.c....it built successfully.
My problem: Why can't I have several files under the same project and run them successfully?
Because you will have the main function declared in both files. I do not know how to tell XCode to build two separate applications..
You can't have 2 main functions
Edit: each c program has to contain only one main function.
Other files of that program can not implement another main function.
Related
So i have been using VSCode and back there I had only to make new file save as .C and when I pressed the "run" it automaticly created the "exe" file by themself.
This method is important to me because as I am on programming class, I have the A B C D E F ... exercise to run.
I heard Clion was one of the best tools out there for C, and i have a student acc so decided to try. But here when I create a new project it only lets me run the first file, tried to add a new source file but it fails.
Is there any solution ?
Thanks
CLion uses CMake as makefile generator. I think this question is about CMake "language" and not about CLion. With CMake you can create multiple executables but there is no automatic way to do it.
You can't have more than one source file if you want it to run independently in the same project. As you may know there should be only one main function where the program initiates.
I'm using Xcode to write C code. Firstly, it took me for ages to figure out that I need to go to Product->Edit Scheme->Select Executable in order to run C executables.
I have a lot of source files and each one has a main so is there any way to run C source files by just selecting them from left-hand pane and pressing Run? Is there a simpler workflow?
Add new target and select the source file which you need and assign to this target and run.
I'm new to programming and currently I'm learning C programming. I'm writing codes on the code blocks and in it using GCC compiler. When I create a new project, (as you know it creates main.c file with it) and due to that I'm not able to compile another file in that project.
File 1:
#include<stdio.h>
int main()
{
int a,b,c,d;
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
d=a;
if(b>d)
d=b;
if(c>d)
d=c;
printf("\n The maximum of three numbers is %d",d);
}
File 2: main.c
#include <stdio.h>
int main()
{
printf("Hello world!\n");
return 0;
}
When I compile the first programme, it shows the following error:
multiple definition of 'main'
first defined here
I've searched every where I could and I'm not able to solve this. In one of the answers here on stack overflow, someone had suggested to write this in
(Project->Build options...->Linker settings (tab))
-Wl,--allow-multiple-definition
When I wrote it, there were no errors. But it wasn't able to run my File 1 and instead, it runs that main.c file. Even when I close the main.c file, it opens there again and runs main.c file which gives the output "Hello World!".
Initially when I was using code blocks there were no such errors. I don't know why this is happening and I've not much knowledge about compilers.
As noted in comments you can only have one main function.
So when you start a new project you need to replace the main.c file with the main.c file you want to use. Or you can edit the 'hello world' main.c program.
When you start a new project in code::blocks you can get a new directory with a simple program that prints 'Hello World'. This file is usually main.c. You need to edit this file or replace it. The reason that code::blocks puts this simple main.c program in the new project is so that you can compile it and test your system without having to write a new program.
Some computer languages allow you to use the same function name for different functions ( which are identified by their parameters and sometimes return types ). That's called overloading. C does not allow this. Functions in C must have unique names.
The main() function is a special one in C as it is used as the standard entry point for applications. That is, the main() function will be called first and your application should start and (typically) end in that function.
As a beginner I would suggest you avoid automated editor features that create and build projects for you. You will miss out on learning how things work doing that. Use an editor to start from empty files and learn how they all connect and how to use the compiler from the command line. The command line is something every beginner should start from, IMO.
It may be harder to learn, but it will give you a much better feel for what is going on.
I guess what you maybe trying to do is have multiple sandbox "gists" that you may wanna run all as their own main function. If that is the case, then just close your project and open the files directly. As long as they are not in a project, they will run fine.
I am trying to debug my application which use one static builded library.
I want to set break points in my library so i tried to set it using below command :
break TS.cpp:600(FIle name:line no)
but it says
No source file named TS.cpp.
Make breakpoint pending on future shared library load?(y or [n])
so I presses y here (I came to know after browsing internet) but after pressing y gdb is not stopping at my break point and it completed executing program.
Why GDB is not stopped at my break point??
Any input is highly appreciated.
No source file named TS.cpp
This means one of two things:
either the file TS.cpp was not compiled with -g (or equivalently TS.o has been stripped), or
the file TS.o was not linked into the application.
Since you are seeing prints from that source, it's a safe bet that #1 is the actual root cause.
info sources command shows only my application.c and not the files of my library
That is another confirmation that #1 is the root cause.
The problem in your case is with source mapping. It normally happens when application is compiled at some other machine and you are debugging it on some other machine where source location is different.
You can specify source path using directory command of gdb. e.g. if your sources are in /home/taimoor/testApp/src, you can do following:
(gdb) directory /home/taimoor/testApp/src
I have two c files, client.c and server.c. From the command line, I can just compile both, open two terminal tabs, and run each in one, so that I can see how they interact with each other. Each one of them consists only of one function, main, which runs forever until terminated manually.
I'd like to speed up the process by running these from Xcode. I created a new project called 'exercise' which contains these two files as well as a number of text and other files. However, when I click on run, it gives me a linker error saying the symbol _main is duplicate. How can I specify to XCode which c file I want to run, or better yet, tell it to run both files independently when I click run?