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
Solved

Linux C++ Makefile

Posted on 2014-09-10
7
206 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 84

Accepted Solution

by:
ozo earned 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

840 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