943,691 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1471
  • C++ RSS
Mar 13th, 2009
0

problem in makefile

Expand Post »
hi guys im trying to write a makefile which contains :two cpp files and two header files.
now i've put cppfiles in a folder called source whose path is: /root/workspace/source

and header files in a folder called header whose path is:
/root/workspace/makedemo/header

my makefile is in the path:/root/workspace/makedemo

my makefile was like this:

C++ Syntax (Toggle Plain Text)
  1. HEADERS = $(shell /root/workspace/makedemo/header ls *.h)
  2. SOURCES = $(shell /root/workspace/source ls *.cpp)
  3.  
  4. COMPILERFLAGS = -W -Wall
  5. DEBUGFLAGS = -g
  6. CPPCOMPILER = g++
  7.  
  8. INCLUDES = -I.
  9.  
  10.  
  11. OBJS = $(SOURCES:.cpp=.o)
  12.  
  13. BINARY = output
  14.  
  15. all: $(BINARY)
  16.  
  17. $(BINARY): $(OBJS)
  18. $(CPPCOMPILER) $(COMPILERFLAGS) $(INCLUDES) -o $(BINARY) $(OBJS)
  19. depend:
  20. makedepend -f- -- $(SOURCES) > .depend_file
  21. clean:
  22. rm -rf *.o .depend_file $(BINARY) *~
  23.  
  24. #DO NOT DELETE

im sure that i've given the correct path but it is showing errors like this:

[root@localhost makedemo]# make
/bin/sh: /root/workspace/source: is a directory
/bin/sh: /root/workspace/source: is a directory
g++ -W -Wall -I. -o output
g++: no input files
make: *** [output] Error 1

any help appreciated...
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Mar 13th, 2009
0

Re: problem in makefile

Your shell commands are off. Try this:
C++ Syntax (Toggle Plain Text)
  1. HEADERS = $(shell ls /root/workspace/makedemo/header/*.h)
  2. SOURCES = $(shell ls /root/workspace/source/*.cpp)
  3.  
Reputation Points: 57
Solved Threads: 64
Posting Whiz in Training
mcriscolo is offline Offline
218 posts
since Apr 2008
Mar 13th, 2009
0

Re: problem in makefile

hi mcriscolo ,im still having problem after changing it to the way u have told..
error was like this:
[root@localhost makedemo]# make
/bin/sh: /root/workspace/source/: is a directory
/bin/sh: /root/workspace/source/: is a directory
g++ -W -Wall -I. -o output
g++: no input files
make: *** [output] Error 1
Last edited by karthik.c; Mar 13th, 2009 at 9:59 am.
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Mar 13th, 2009
0

Re: problem in makefile

Please post your updated makefile and I'll take a look.
Reputation Points: 57
Solved Threads: 64
Posting Whiz in Training
mcriscolo is offline Offline
218 posts
since Apr 2008
Mar 14th, 2009
0

Re: problem in makefile

hi mcriscolo ,im sorry i actually misplaced 'ls' in the SOURCES and HEADERS path but later when i gave make command it showed some errors like this:

[root@localhost makedemo]# make
g++ -c -o /root/workspace/source/Main.o /root/workspace/source/Main.cpp
/root/workspace/source/Main.cpp:1:24: SourceData.H: No such file or directory
/root/workspace/source/Main.cpp:2:22: TestFile.H: No such file or directory
/root/workspace/source/Main.cpp: In function `int main()':
/root/workspace/source/Main.cpp:5: error: `TestFile' was not declared in this scope
/root/workspace/source/Main.cpp:5: error: expected `;' before "obj"
/root/workspace/source/Main.cpp:6: error: `SourceData' was not declared in this scope
/root/workspace/source/Main.cpp:6: error: expected `;' before "sd"
/root/workspace/source/Main.cpp:7: error: `sd' was not declared in this scope
/root/workspace/source/Main.cpp:9: error: `obj' was not declared in this scope
make: *** [/root/workspace/source/Main.o] Error 1

im sure i've no problem in other header and source files as i compiled and ran successfully when all was in the same folder(including makefile).

i've used makedepend tool for dependency and i think above problem is arising because of dependency issue.i would also like to know where(in which folder) .o files will be created when im compiling ?

any way i've also included source and header file coding here...
source:
Main.cpp:
C++ Syntax (Toggle Plain Text)
  1. #include "SourceData.H"
  2. #include "TestFile.H"
  3. int main()
  4. {
  5. TestFile obj;
  6. SourceData sd;
  7. sd.rollNum = 101;
  8. sd.name = "Shiva";
  9. obj.Display(sd);
  10. return 0;
  11. }

TestFile.cpp:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "TestFile.H"
  3. void TestFile::Display(SourceData &d)
  4. {
  5. std::cout<<d.ToString()<<std::endl;
  6. }

Header:
SourceData.H
C++ Syntax (Toggle Plain Text)
  1. #ifndef _SORCEDATA_H_
  2. #define _SORCEDATA_H_
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. struct SourceData
  7. {
  8. int rollNum;
  9. std::string name;
  10. std::string ToString()
  11. {
  12. std::stringstream sst;
  13. sst<<"RollNum = "<<rollNum<<"\n";
  14. sst<<"Name = "<<name<<"\n";
  15. return sst.str();
  16. }
  17. };
  18. #endif
TestFile.H:
C++ Syntax (Toggle Plain Text)
  1. #ifndef _TESTF1_H_
  2. #define _TESTF1_H_
  3.  
  4. #include "SourceData.H"
  5. class TestFile
  6. {
  7. public:
  8. void Display(SourceData &d);
  9. };
  10. #endif
and my makefile was like this:
C++ Syntax (Toggle Plain Text)
  1. HEADERS = $(shell ls/root/workspace/makedemo/header/ *.H)
  2. SOURCES = $(shell ls/root/workspace/source/ *.cpp)
  3.  
  4. COMPILERFLAGS = -W -Wall
  5. DEBUGFLAGS = -g
  6. CPPCOMPILER = g++
  7.  
  8. INCLUDES = -I.
  9.  
  10.  
  11. OBJS = $(SOURCES:.cpp=.o)
  12.  
  13. BINARY = output
  14.  
  15. all: $(BINARY)
  16.  
  17. $(BINARY): $(OBJS)
  18. $(CPPCOMPILER) $(COMPILERFLAGS) $(INCLUDES) -o $(BINARY) $(OBJS)
  19. depend:
  20. makedepend -f- -- $(SOURCES) > .depend_file
  21. clean:
  22. rm -rf *.o .depend_file $(BINARY) *~
  23.  
  24. #DO NOT DELETE
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Mar 14th, 2009
0

Re: problem in makefile

Try this makefile:
C++ Syntax (Toggle Plain Text)
  1. TOP_DIR = /root/workspace
  2. S_DIR = $(TOP_DIR)/source
  3. H_DIR = $(TOP_DIR)/makedemo/header
  4.  
  5. HEADERS = $(shell ls $(H_DIR)/*.H)
  6. SOURCES = $(shell ls $(S_DIR)/*.cpp)
  7.  
  8. COMPILERFLAGS = -W -Wall -c
  9. DEBUGFLAGS = -g
  10. CPPCOMPILER = g++
  11.  
  12. INCLUDES = -I$(H_DIR)
  13.  
  14. OBJS = $(SOURCES:.cpp=.o)
  15.  
  16. BINARY = output
  17.  
  18. all: $(BINARY)
  19.  
  20. $(BINARY): $(OBJS)
  21.   $(CPPCOMPILER) -o $(BINARY) $(OBJS)
  22.  
  23. .cpp.o: $(SOURCES) $(HEADERS)
  24.   cd $(S_DIR); $(CPPCOMPILER) $(COMPILERFLAGS) $(INCLUDES) $<
  25.  
  26. depend:
  27.   makedepend $(INCLUDES) -f- -- $(SOURCES) > .depend_file
  28. clean:
  29.   rm -rf $(S_DIR)/*.o .depend_file $(BINARY) *~
  30.  
  31. #DO NOT DELETE
  32.  
Your original makefile was not pointing to the proper include file, so it was not picking up your headers. That was the source of your errors. Here, I've suggested some changes that make the file somewhat cleaner (at least to me). See if they work for you.

I made the changes to the top, by adding some variables for the directories, since I'd have to go to them (for example, in the "clean" rule).

The other major addition is the ".cpp.o" rule to build the objects prior to linking the executable. I found that the one rule in the original Makefile was not picking up the stuff in COMPILERFLAGS, so I knew it was hitting a default rule for the .cpp items.

The *.o files will, by default, be dropped in the current folder where make is being run (in your example, /root/workspace/makedemo), but then the variables for the object files that have the paths pre-pended would fail during the link step. I fixed that by performing a "cd $(S_DIR)" prior to the compile step to drop the *.o files in the "source" folder. The executable ("output"), however, will still fall into the "makedemo" folder.

I didn't mess with "makedepend" - perhaps someone else knows more about that. There also is one thing I wasn't able to get working - if you "touch" one of the header files, when you make it won't rebuild. I'll look into that.

Hope this helps!
Reputation Points: 57
Solved Threads: 64
Posting Whiz in Training
mcriscolo is offline Offline
218 posts
since Apr 2008
Mar 16th, 2009
0

Re: problem in makefile

hi mcriscolo, i tried your make file and its working fine without any problem,but still i've some doubts in it to clarify...
>>The other major addition is the ".cpp.o" rule to build the objects prior to linking the executable.
C++ Syntax (Toggle Plain Text)
  1. $(BINARY): $(OBJS)
  2. $(CPPCOMPILER) -o $(BINARY) $(OBJS)
the above rule is what which links all the object files to produce executable ,but you have included .cpp.o rule for creating object file only after the target rule and makfile is running fine without any problem(even though i tried other way by writting .cpp.o rule before target rule and as expected it didnt show any error there also)so why is that it is not showing any error?

>>I found that the one rule in the original Makefile was not picking up the stuff in COMPILERFLAGS, so I knew it was hitting a default rule for the .cpp items.

>>but then the variables for the object files that have the paths pre-pended would fail during the link step. I fixed that by performing a "cd $(S_DIR)" prior to the compile step to drop the *.o files in the "source" folder.

im sorry if i sound stupid but still cant help asking you,i didnt get what you was saying here and also i'vent used .cpp.o rule till now ...and when i've created .o files already using the suffix replacement like this:

OBJS = $(SOURCES:.cpp=.o)

why should i use .cpp.o rule again for creating object files?so can you explain me why its used here(ofcourse i know its for creating object files but would like to know the meaning of the entire line and each macros used in the below rule...)
C++ Syntax (Toggle Plain Text)
  1. .cpp.o: $(SOURCES) $(HEADERS)
  2. cd $(S_DIR); $(CPPCOMPILER) $(COMPILERFLAGS) $(INCLUDES) $<
i've one last question ,in this makefile scenario is such that all the source files are in one folder called source and headers in folder called headers(but both these folders in diff directories),now suppose if i have many sourcefiles and headerfiles residing in other directories other than in the given folder called source and headers ,how do i mention their path in makefile?

should i have to mention the path for each files in diff directories as diff macros or is there any other way for doing that?
thanks in advance...
Last edited by karthik.c; Mar 16th, 2009 at 3:26 am.
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Mar 16th, 2009
0

Re: problem in makefile

To answer the question about adding the ".cpp.o" rule: When I ran your makefile as is, I was seeing the compilation of the .cpp files (trying to make the output .o files) working, but it was not picking up the $(COMPILERFLAGS) variable. That is, this rule in your makefile:
C++ Syntax (Toggle Plain Text)
  1. $(BINARY): $(OBJS)
  2. $(CPPCOMPILER) $(COMPILERFLAGS) $(INCLUDES) -o $(BINARY)
when compiling the .cpp sources, was producing this output:
C++ Syntax (Toggle Plain Text)
  1. g++ -c -o /root/workspace/source/Main.o /root/workspace/source/Main.cpp
Where is the "-W -Wall"? Where is the "include" directive? It was not in the output of the command make was executing. This is why you were getting the build errors - it didn't know where to find the header files. That's because make was executing a default rule for the .cpp files, and not using the directives in the rule for building the executable. Once I added the ".cpp.o" rule, everything built - almost. The .cpp built and made the correct .o files, but when it was time to make the executable, I got this output:
C++ Syntax (Toggle Plain Text)
  1. g++ -W -Wall -c -I/root/workspace/makedemo/header -o output /root/workspace/source/Main.o /root/workspace/source/TestFile.o
That's because I had to put a "-c" into the COMPILERFLAGS variable, and now that it's trying to build the executable, the "-c" is trying to output object code rather than perform the link. This required the removal of the $(COMPILERFLAGS) variable from the rule that was building the executable.

Some of the macros for make: "$<" is the name of the file that caused the rule to be executed, so in the case of the .cpp.o rule, this would be a .cpp file.

As far as having many different source directories and headers, those have to be specified separately (or at least in separate variables). When we do that kind of stuff, we're usually building libraries, so each library would have its own makefile, and then the build for the executable would have its makefile to pull in the required libraries.

Hope this helps.
Reputation Points: 57
Solved Threads: 64
Posting Whiz in Training
mcriscolo is offline Offline
218 posts
since Apr 2008
Mar 17th, 2009
0

Re: problem in makefile

thanx a lot mcriscolo,your explanation was satisfactory ...but i would also like to know if there is any sequential order by which rules defined in the makefile are evaluated or it doesnt matter here in this file?
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: trying to run a string as a command
Next Thread in C++ Forum Timeline: Applying heat flux on a tube surface





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC