Solved

Linux C++ Makefile

Posted on 2014-09-10
7
199 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now