ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi
I written four C programs each does different function when received message from client
Common thing for all C programs are socket which act as server and execute function in thread.
Now what I feel there are repitition of code.I want to avoid this.
For example Include directory will be there were wrapper is written for socket etc.
Also I want to use makefile
Please show me some example code so that I can use as reference.
Firstly it sort of matters if these four files link to create one image or if you intend to create multiple targets. As HMW notes, read up on Makefiles and begin with rudimentary ones. There are plenty of examples for how to make the most simple of Makefiles.
And a VERY IMPORTANT make file thing you need to know is PUNCTUATION. Note that when you indent you NEED TO USE TABS AND NOT WHITESPACES. I recommend you use a Linux or Unix editor to create your Makefiles and also ensure that the editor is not configured to replace TAB with spaces.
I have a simple legacy Makefile which I refer to all the time for the basics as well as my list of GCC compiler flags that I like to retain and use. They improve my code because they check for lots of warning conditions and thus help me to not create suspect code. I'll be happy to post that example, but I feel you first ought to make a reasonable attempt to show what you've tried.
The best way to create a makefile is to look at some makefiles. Maybe start with the makefiles used to build make. Hopefully they are up to date and correct. Put all your code in one file and remove duplicate code?
@rtmistler I have one makefile
I made directories as Source, Include, Lib
Source :-cpp files where functions are written
Include:-header files for source cpp
Lib:- After compilation .o files should go in this directory
Source,Include,Lib will be common for all 4 programs I mentioned in first post
Apart from that my first main application appdownload.cpp file will be in project directory along with its include.h file.
I think it's an excellent effort and in fact there's little you need assistance with.
Here are some added suggestions:
Start the file with #!/bin/sh, it will help you to be able to use shell variables and commands, for instance I declare START to be `pwd` in case I need to move around directories as part of my make flow. Otherwise I think it will assume a shell no matter what anyways, but I prefer to make that a specific declaration.
The only other minor suggestion is that I use only "rm -f" not "rm -rf" for my clean. I'm doubtful -rf is needed and I'd stay away from recursively removing directories just in case something weird happens and suddenly "Source" gets removed, along with all the underlying code source files.
I think at this point a lot of this are style differences between one programmer versus another.
What I do is add incrementally new ideas as I see them in other examples or develop them when I need too.
If you use these tools to discover dependencies and to construct your makefiles, it is much easier to be certain that your makefiles remain accurate as the program evolves.
Last edited by sundialsvcs; 10-14-2015 at 08:26 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.