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.
Related
I have an entire library made in C. It has almost 10 folders with a lot of files.
I have created a filename.c file in root folder and trying to compile it in mac using gcc test.c -o test however its not including header files. Generally I have to add all the header files gcc test.c libaudio.c -o test
How can I compile entire project instead of just one file.
Makefiles will solve your problem. You can create your own rules to clear the project (remove the generated files), build the project indicating where is your compiler (compile the source files located in some specific path, extension, etc), set the output path and so on, without typing a large compilation order.
https://www.gnu.org/software/make/manual/make.html
Edit: There you will be able to find how to add shared, static or raw libraries to your proyect through makefiles.
Use a Makefile. make the utility the reads the configuration within the Makefile will automate the running of the individual commands, such that you only need to name the item you wish to be rebuilt.
make myprogram
And make will use the dependency information stored in the Makefile's rules to determine what other elements are "out of date", rebuilding those and assembling them into myprogram.
This is a decent "first time" tutorial for "make".
Here is the full blown documentation for "make"
Once you master the concepts within make, you can then use other tools that make maintaining Makefiles either easier, more portable, or both.
Some tools that improve upon "make" include "cmake", "automake", "the autotools collection", "scons", "waf", "rake", "doit", "ninja", "tup", "redo", and "sake". There are more, and some are programming language specific, or limited to a particular enviornment.
The reason I recommend "make" over the others is because "make" is a baseline that will always be present, and the features in the other tools are often not understood or recognized to be needed until you get enough experience with "make".
In C, the concept of project is not part of the language, it depends generally of the tools / platform / library you have to build.
On Linux based platforms, you may have a makefile describing the project, or the library may have a cmake script.
You should be able to find the build instructions in you library documentation.
I definitely recommend the make approach as it is scalable.
If you really only have a couple of files, gcc will accept multiple .c files on the command line and link them all to generate one executable.
I have two files in my project.
One is a ".c" file, one is a ".asm" file. First, I compile both the files into two ".o" object files and then link together using a custom linker script.
Currently, I am using a simple bash script with manual commands to build the project, but as the project size is expected to increase, I was thinking of moving the project to using cmake as its primary build system.
I have searched documentation of cmake and googled for a lot of time, although I have shortlisted some of the variables in cmake which might prove to be useful in this case, I am unable to find any sample explaining how to achieve this.
Can anybody provide me with a brief sample code on how should I go about achieving this ?
You asked for an example? Here's one from the cmake wiki
set(mySrcs foo.c bar.c)
set(can_use_assembler FALSE)
# test wether it is a x86 machine and as/gas is available
if("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "i.86")
enable_language(ASM-ATT)
if(CMAKE_ASM-ATT_COMPILER_WORKS)
set(can_use_assembler TRUE)
set(mySrcs ${mySrcs} codec_i86.s)
endif(CMAKE_ASM-ATT_COMPILER_WORKS)
endif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "i.86")
# no assembler found
if(not can_use_assembler)
set(mySrcs ${mySrcs} codec_generic.c)
endif(not can_use_assembler)
add_executable(player ${mySrcs})
Because of the way that CMake works, assembling and linking are grouped together in the same step (from the programmer's perspective at least). So add_executable automatically handles everything for you.
If you want to include a linker script, that gets more difficult with CMake. The best place to look into it is over here. Since the examples and instructions are too long to copy out in the SO answer box, I'll leave the reading to you.
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.
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.
I'm in the process of converting a project to Eclipse CDT, it consists of 2 (static) libraries and produces about 12 binaries, a few of the binaries have 2-3 different build configurations, and is built by scons
How should I structure this in an Eclipse workspace ? 1 project for everything ? 1 project for each of the binaries/libs ? Something else ?
I'd suggest you use CMAKE for this problem, It should be able target target the Eclipse build system. If not, it can generate a normal 'make' config for you. It is far better to go down this route since its more portable in the long term, and writing a hierarchical build system is quite straight forward.
I personally have used Eclipse CDT before, but only in makefile mode i.e. to build anything I'd manually run the makefile. Basically I used Eclipse as a glorified editor. Here's how I worked things:
Everything part of the otherall solution came under the same workspace. Each library/binary was its own directory and project, so that I could make each as required. I also had a separate folder (project) for tests with a makefile that built all the test exes I wanted to run so I could do valgrinds on simple bits of it.
As I said, I used make and not Eclipse CDT's built-in building routines - to that end I'd say it really doesn't matter how you structure it - do whatever makes sense / conforms best to the UNIX principles.