?
Solved

Linux C++ Makefile

Posted on 2014-09-10
7
Medium Priority
?
210 Views
Last Modified: 2015-01-05
I have two issues
1) all my files are in the same directory but when i compile  (G++) it does not find my .h file. Only if I include the fulll path for the .h file in the include statement does it work. Why is the compile not finding it in the same directory?

2) When I run the following make file I do not get and exe file?

all: Magic.x
Magic.x: test.o test.o
        g++ -o test.x test.o test.o
test.o: test.cpp
        g++ -c test.cpp
Magic.o: Magic.cpp
        g++ -c Magic.cpp
clean:
        rm -rf *.o *~ *.x
0
Comment
Question by:pcomb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40314202
How did you include your .h file?  with <> or ""?
How are your include paths defined?

Did you get any errors running make?
g++ -o test.x test.o test.o
would either define main 0 times or 2 times,
executables should declare it declared exactly once
0
 

Author Comment

by:pcomb
ID: 40314211
my include file is
#include </home/abc/xyz/proj/test.h>

Not sure I follow the questino on include path?

No errors

0 or 2 times not sure what you mean for this?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40314221
test.o is linked twice,
if test.o contains main(), it will be a duplicate symbol, and the link will fail
if test.o does not contain  main(), it will be an undefined symbol, and the link will fail
when you ran make, it should have given you an error message to tell you which way it failed.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 40314236
# include <h-char-sequence> new-line
searches a sequence of implementation-defined places for a header identified uniquely by
the specified sequence between the < and > delimiters,
these are usually system include directories.

# include "q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the source file identified
by the specified sequence between the " delimiters. The named source file is searched
for in an implementation-defined manner.
these searches are usually relative to the current directory
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40320523
>>    g++ -c Magic.cpp
The above creates Magic.o, but there is no line that uses Magic.o.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40320526
>>  all: Magic.x
>>  Magic.x: test.o test.o
>>           g++ -o test.x test.o test.o         // this is trying to create an executable called, test.x

Without seeing an outline of your code, I can only take a guess. Assuming that you want only one executable, Magic.x, and not an executable, test.x, and assuming that there is one main() function in either test.o or Magic.o, then try this:

all: Magic.x
Magic.x: Magic.o test.o          // Magic.x executable is dependent on Magic.o and test.o
         g++ -o Magic.x  Magic.o  test.o      // build Magic.x from Magic.o and test.o
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question