LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2012, 11:28 AM   #1
texton
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Rep: Reputation: Disabled
Trouble compiling in the right "format" with Makefile?


Hi all.

I am trying to cross compile for a arm processor, from my linux running PC. Here is my Makefile code:

------------------------------------------------------
SOURCES=main.cpp part1.cpp part2.cpp
OBJECTS=${SOURCES:.cpp=.o}
EXECUTABLES=main_tgt
CC=arm-angstrom-linux-gnueabi-g++

all: ${OBJECTS}
${CC} -o ${EXECUTABLES} ${OBJECTS}

parts: ${SOURCES}
${CC} -c ${SOURCES}

clean:
rm ${EXECUTABLE} ${OBJECTS}
---------------------------------------------------------------

As you see i want to compile with the arm-angstrom...-g++ compiler, but when i do compile the objects files made is for Intel80386 ????.. And therefore the executable never gets made?.

Someone know why the generated object files becomes Inten80386 objectfiles instead of arm?
 
Old 02-22-2012, 01:27 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Although you posted this as a question about Makefiles, I question whether you actually have a properly installed ARM Angstrom cross toolchain. Can you run the specified compiler outside of a Makefile, and still create ARM binaries? How are you determining that the binaries are x86 format?

--- rod.
 
Old 02-22-2012, 01:42 PM   #3
texton
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
I have compiled before without using Makefile, and then it compiles just fine. Actually I have also compiled a "Hello_world.cpp" with a Makefile similar to the one shown, just with Hello_world.cpp as the only document in the variable SOURCES.

I determine the file format by typing:
>$ file main.o

In my project folder I actually have 2 makefiles: Makefile.host and Makefile.target where Makefile.host works perfectly fine, but Makefile.host wont
 
Old 02-22-2012, 01:43 PM   #4
texton
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
And I remember to delete all the object files before running a Makefile.
 
Old 02-22-2012, 02:00 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, when the make runs, do you see the correct version of gcc being used? Have you removed the old object code (make clean) and re-tried? Is the correct compiler in $PATH (you don't have a fully qualified filespec for $CC).

--- rod.

BTW: you must have mistyped when you wrote: 'Makefile.host works perfectly fine, but Makefile.host wont'
 
Old 02-22-2012, 02:37 PM   #6
texton
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes i clean first. And i think that the path is known.. I have attached a screenshot of the terminal error if that help u help me?
Attached Thumbnails
Click image for larger version

Name:	Linux.png
Views:	15
Size:	60.5 KB
ID:	9130  
 
Old 02-22-2012, 02:50 PM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Caveat: I don't do any cross-compiling. Forgive me if I misunderstand the process, but...

The output you show indicates that make is relying on implicit rules to create the object files. The implicit rule invokes plain-jane "g++" -- not "arm-angstrom-linux-gnueabi-g++" -- to create the object files.

I assume you need "arm-angstrom-linux-gnueabi-g++" to create those files.

To do so, you need to modify your Makefile and add something like this:
Code:
%.o : %.cpp
        ${CC} -c $<
It appears that your "parts" target may have been what you thought would perform this step.

As an alternative to the above, depending on what your ultimate goal is, you could probably just replace your "parts" target definition with:
Code:
#parts: ${SOURCES}
${OBJECTS} : ${SOURCES}
-or-
Code:
${OBJECTS} : parts
parts: ${SOURCES}
The original approach using "%.o : %.cpp" is the more typical/standard way of doing it in my experience.

EDIT:
Also, assuming you're using GNU make, you probably need to add:
Code:
.PHONY : clean
Which will make sure that the clean target always executes its target instructions when invoked.

Last edited by Dark_Helmet; 02-22-2012 at 03:22 PM.
 
1 members found this post helpful.
Old 02-22-2012, 05:21 PM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I think the OP should consider defining CXX in lieu of using CC, which is for C programs. CXX is interpreted by 'make', along with CXXFLAGS if compiler flags are needed, for building C++ files. This would obviate the need to define a rule to make the object files, since 'make' already has a built-in rule for this.

Last edited by dwhitney67; 02-23-2012 at 04:48 AM.
 
1 members found this post helpful.
Old 02-23-2012, 06:46 AM   #9
texton
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
It works!!.. I deleted the parts target and inserted
Code:
%.o : %.cpp
        ${CC} -c $<
So thanks! .. But what does the 2 lines exactly?

And where should I insert the ".PHONY : clean" line, so the clean target is called every time I run the makefile?

Oh and one more question: How can I save my object files in an other directory?.. for example if my makefile is in home/user/project1 and I want to save my object files in home/user/project1/target?

Last edited by texton; 02-23-2012 at 07:00 AM.
 
Old 02-23-2012, 08:51 AM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by texton View Post
It works!!.. I deleted the parts target and inserted
Code:
%.o : %.cpp
        ${CC} -c $<
So thanks! .. But what does the 2 lines exactly?

And where should I insert the ".PHONY : clean" line, so the clean target is called every time I run the makefile?

Oh and one more question: How can I save my object files in an other directory?.. for example if my makefile is in home/user/project1 and I want to save my object files in home/user/project1/target?
The percent character denotes a wildcard. Thus to make foo.o, 'make' looks for foo.cpp. The $< refers to the dependency, which is %.cpp. The -c option instructs the compiler to build only an object (.o) file.

You really should be using CXX, not CC, for building C++ files.

P.S. For example:
Code:
SOURCES=main.cpp part1.cpp part2.cpp
OBJECTS=${SOURCES:.cpp=.o}
EXECUTABLES=main_tgt
CXX=arm-angstrom-linux-gnueabi-g++

all: ${OBJECTS}
        ${CXX} -o ${EXECUTABLES} $^

clean:
        $(RM) ${EXECUTABLE} ${OBJECTS}

Last edited by dwhitney67; 02-23-2012 at 08:54 AM.
 
Old 02-23-2012, 10:59 AM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
And where should I insert the ".PHONY : clean" line, so the clean target is called every time I run the makefile?
I usually place it right before the clean target itself.

But you may have misunderstood what I meant--my wording was poor. Adding ".PHONY : clean" to the Makefile will force make to execute the instructions for the "clean" target everytime you invoke "make clean." That is, the "phony" designation turns off make's internal decision making for whether the target should or should not be updated.

It will not cause make to execute the clean target commands everytime you run make.

You might be able to accomplish what you're thinking by adding the clean target as a dependency to another target. Though, I think the make documentation says that the order of dependency updates is not guaranteed. So doing so might cause the clean instructions to run after other dependencies have been built. Also, by running clean everytime, you eliminate one of the main reasons for using make to begin with: only re-compiling the parts that have been changed since your last compile.

EDIT:
Collecting your object files in another directory will require more work. I recently came across a post on another forum. The second answer (added by anon and edited by Daryl Spitzer) gives a solution to that particular problem with the use of the patsubst() function provided by make.

Last edited by Dark_Helmet; 02-23-2012 at 01:06 PM.
 
Old 02-23-2012, 12:12 PM   #12
texton
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Okay..

I got it all to work now!.. Thanks to both of you

I gotta say, i rly like the Linux community!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to force "configure" and "Makefile" to look for dependencies in non sys folder? Dstruct0 Linux - Software 2 04-05-2011 08:01 AM
How can I convert makefile argument "toupper" (and "tolower")? daat99 Programming 4 12-04-2010 10:49 AM
Makefile issue: "Makefile.src: File not found" m3rkury Linux - Software 1 02-22-2007 10:15 PM
Having trouble compiling "my 1st program"??? Southpaw76 Programming 7 10-21-2006 12:53 PM
trouble compiling xmms mp4 plugin from source: "no such file.." mayasedai Linux - Software 3 02-01-2005 11:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:28 PM.

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