LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-29-2010, 02:19 PM   #1
usul.the.mouse
LQ Newbie
 
Registered: Mar 2006
Location: Green Bay, WI
Distribution: Ubuntu, Slackware, DSL
Posts: 13

Rep: Reputation: 0
Smile Linux Programming


Hello,

I couldn't find an actual programming forum.
so if this is the wrong place I apologize.

If I am I have questions...

I have been programming for years, and most of it in Visual Stupid.
And I really want to get into programing in the *nix environment.
Are there any good tutorials out there that are gear toward individuals wanting to make the win to *nix transition.

I understand C, C++ syntax. But I have no understanding of make file(s). Most of that kind of thing was handled by VS. I have tried googling this but I either find too little or too much. Nothing I have found seems to be gear towards coders in transitions.

I am thinking emacs or some other NON-IDE environment.

Any help you can offer would be very much appreciated,
so if you would point, boot me in the right direction.

theMouse
 
Old 01-29-2010, 02:41 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
The basics (for C):

Compile source files to executable:

Code:
gcc source1.c source2.c source3.c
The executable will be named "a.out". Do not enter your .h files, just the .c ones.

Compile source files to executable and specifying executable name:

Code:
gcc source1.c source2.c source3.c -o exename
Don't make you executable's name end with .exe, that's just a Windows thing. Linux executables usuallt don't have an extension.

Compile source files to object files, do not link:

Code:
gcc -c source1.c source2.c source3.c
The output files will have the names of the input files, with the extension replaced with .o.

Link object files:

Code:
gcc source1.o source2.o source3.o
The exe will be named a.out.

Link object files, specifying exe name:

Code:
gcc source1.o source2.o source3.o -o exename
C++ works the same, just use the "g++" command instead of the "gcc" command.

Last edited by MTK358; 01-29-2010 at 02:43 PM.
 
1 members found this post helpful.
Old 01-29-2010, 02:46 PM   #3
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Hi usul.the.mouse,

Makefiles are quite simple. They contain many lines of the form:

target: dependency1 dependency2 ...
commands to update target

example:
CPPFLAGS=-g -Iinclude -W -Wall

myprogram: main.o io.o file.o
g++ -o myprogram main.o io.o file.o

Put these lines into a file called Makefile and run make in the directory. make will look for a file called Makefile, makefile or (for gmake) GNUMakefile. This file will be parsed and the defined rules are evaluated. In the above example this looks as follows:

make will find a rule: the file 'myprogram' depends on 'main.o', 'io.o' and 'file.o'. If any of these files is newer than myprogram, g++ will be called to update myprogram. But because 'myprogram' depends on 'main.o', 'main.o' will be checked for update too. Because you did not define a rule for 'main.o', a built-in rule will be used. In this case, if e.g. 'main.cpp' exists, g++ will be called with the flags CPPFLAGS to create main.o

for a tutorial see, e.g.
http://www.eng.hawaii.edu/Tutor/Make/

for a reference: "info make"

An example makefile for small programs can be
Code:
#
# Makefile for my small program
#

CPPFLAGS=-Iinclude -O2 -W -Wall
SMALLPROGRAM_O=main.o file1.o hello.o file2.o
SMALLPROGRAM_O+=file3.o gui.o
SMALLPROGRAM_LIBS=-lm -lX11 -lXm -lXaw6

all: smallprogram

smallprogram: $(SMALLPROGRAM_O)
  g++ -o $@ $(SMALLPROGRAM_O) $(SMALLPROGRAM_LIBS)
The basic is to know how to call the compiler/linker/assembler from command line. See "info gcc", "info ld" or "info as" for these information.
 
1 members found this post helpful.
Old 01-29-2010, 04:05 PM   #4
usul.the.mouse
LQ Newbie
 
Registered: Mar 2006
Location: Green Bay, WI
Distribution: Ubuntu, Slackware, DSL
Posts: 13

Original Poster
Rep: Reputation: 0
Next Question :)

I am at work (in Visual Stupid LOL)
and I have yet to info/man gcc,ld,as, make etc yet.

In winworld there are (dll or class libraries). in *nix they are just libraries right?
How do these get compiled and referenced ("linked???")

for example I want to write an program:
"doessomething" but I want "doessomething" to have a command line interface and a xwindows one.

So I would like to...
have a common library for all the something that "doessomething" does.
and two programs

so I want:
"doessomethinglib"
"doessomething" uses "doessomethinglib"
"xdoessomething" uses "doessomethinglib"


In Visual Stupid I would
Create one solution with
Class Lib uses "doessomething.dll"
a console app "doessomething.exe"
a windows app "xdoessomething.exe"

All the building and referencing (linking?) is all done by the ide.

I am assuminig in *nix and gcc or g++ I would have to write and build my own "makefile" to do the build which is kewl. this is what I want to learn.

If anyone understands my babbling, and can (has the interest and time) put together a "super simple sample example" I learn by playing/breaking more than I do by reading.

Thanks, I really appreciate it.

Last edited by usul.the.mouse; 01-29-2010 at 04:07 PM.
 
Old 01-29-2010, 05:39 PM   #5
phlyer
Member
 
Registered: Jan 2010
Distribution: CentOS, Vector Linux, Scientific Linux, Rocks
Posts: 36

Rep: Reputation: 17
you can find a lot of man pages in HTML, no UNIX or GNU required

make man page

You should get used to Linux and UNIX systems being case-sensitive.
"Makefile" and "makefile" are actually two different files.


gcc man page

The GNU C compiler (the C++ one as well) invokes the linker (ld).

ld man page

Libraries can be static or dynamic. The latter ones would be the
equivalent to a DLL.

Static libraries are referred to as archive. Their file name should
end in ".a", but doesn't have to.

Dynamic ones are called Shared Objects, and their name should end
in ".so", and again, doesn't have to.

A pretty simple example can be found here.

All this didn't address the make file yet.

More news at 11

Last edited by phlyer; 01-29-2010 at 05:52 PM.
 
Old 01-29-2010, 06:52 PM   #7
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 129Reputation: 129
Don't mess with makefile, we are in 2010, not in 1995, higher level tools have now reached a superior state to old makefiles. Even with 5 years of Linux programming and clone to a decade of using, I don't even understand them. Why?

-Because gcc/g++ command are simple for small apps
-IDE generate them for you like in Visual Studio
-CMake is better and easier than the 2 above.

KDE/Qt is really well designed and in some way (signals and slots) superior to .NET C++. My advice would be to learn to use it. You get a nice IDE called Qt creator or use the much more advanced, but much more complicated KDevelop, an interface designer and all you need. For hard core programmer, vi and some advanced notepad with embeded terminal (Kate) are there too, I prefer the later to an IDE, but that's just taste or habit, I don't know nor does I care.

KDE have a good bunch of started guide. First, you can have a sanboxed development environement isolated from your system, you you can use all latest and greatest SVN/Git build of everything:
http://techbase.kde.org/Getting_Started/Build/KDE4

Then, you can learn the basic inner working of a KDE application:
http://techbase.kde.org/Development/Tutorials#Basics

Then, at last, you will release that the doc is actually a really good tool:
http://doc.trolltech.com
 
Old 01-29-2010, 07:03 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I once tried Qt programming, and I totally hated the signals-and-stots thing. But you can try a few things and see what you like.
 
Old 01-30-2010, 03:09 AM   #9
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
If you really want to use plain Makefiles and not tools like CMake, autoconf and automake. I found an example here

http://users.ph.tum.de/no34bij/lp.tar.gz

This archive contains a Makefile and several c++ source files. The makefile creates a shared library "libsimple.so" and links two programs against it. Maybe it'll help you. Just unpack the archive and type make in the top directory.

In general there is no difference between gui and console programs in linux, except for the libraries they use. So if you are familiar with creating console programs and with a Gui library like Gtk or Qt there should be no problems creating gui programs.
 
Old 02-01-2010, 08:32 AM   #10
usul.the.mouse
LQ Newbie
 
Registered: Mar 2006
Location: Green Bay, WI
Distribution: Ubuntu, Slackware, DSL
Posts: 13

Original Poster
Rep: Reputation: 0
Why?

Quote:
Originally Posted by Elv13 View Post
Don't mess with makefile, we are in 2010, not in 1995, higher level tools have now reached a superior state to old makefiles. Even with 5 years of Linux programming and clone to a decade of using, I don't even understand them. Why?

Because some of the programing I want to do is commandline based,
and on Minix3. So IDE is not available, or at least I don't think it is.

And I am defiantly not ready to to specialize QT vs GTK

Besides its good to understand what is going on, I think.
 
Old 02-01-2010, 05:03 PM   #11
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Quote:
Originally Posted by usul.the.mouse View Post
I have been programming for years, and most of it in Visual Stupid.
Ha ha! "Visual Stupid!"

Thank you, you just made my day.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration