Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Source codes that compile with command line but not in eclipse
Source codes that compile with command line but not in eclipse [message #1775760] Sun, 05 November 2017 20:27 Go to next message
J M is currently offline J MFriend
Messages: 7
Registered: November 2017
Junior Member
Hello everyone,

This is the first time I use Eclipse for C++ codes and I've been stuck for several hours in compilation errors. When I type "make" in a Linux terminal everything compiles just fine but in eclipse it doesn't work and I get the error "make: *** no rule to make target 'all' eclipse". You'll find attached a screenshot of my eclipse project and here's my makefile code :

# Folders
#LIBBIN = ../bin

# Compiler
C=gcc-4.7
CXX=g++-4.7
# Libraries
hdrdir = -I/usr/local/include/  #-I/usr/local/opt/jpeg/include
libdir = -L/usr/local/lib/      #-L/usr/local/opt/jpeg/lib


# Flags
CFLAGS    = -O3 -std=c99 $(hrdir) $(libdir) -fpermissive
COPT      = -lm -O3 -funroll-loops -fomit-frame-pointer -ffast-math -ftree-vectorize -fpermissive
CXXFLAGS += $(COPT)  $(hdrdir) -pedantic -Wall -Wextra -Wno-write-strings -Wno-deprecated
LDFLAGS  += $(CXXFLAGS) $(libdir) -lpng -ltiff  -ljpeg -lfftw3 -lfftw3f -lfftw3l


# use openMP with `make OMP=1`
ifdef OMP
CFLAGS	 += -fopenmp
CXXFLAGS += -fopenmp
LDFLAGS  += -fopenmp
else
CFLAGS	 += -Wno-unknown-pragmas
CXXFLAGS += -Wno-unknown-pragmas
endif


ifdef DEBUG
CFLAGS	+= -g
CXXFLAGS += -g
LDFLAGS += -g
endif


# Objects
OBJC = libImageFormats.o
OBJ	= libBasic.o libImage.o libImageFormatPM.o libHyperspectral.o
BIN =


default: $(OBJC) $(OBJ) $(BIN)

$(OBJC) : %.o : %.c
	$(C) -c $(CFLAGS)   $< -o $@


$(OBJ) : %.o : %.cpp
	$(CXX) -c $(CXXFLAGS)   $< -o $@


$(BIN) : % : %.o
	$(CXX) $(LDFLAGS)  -o $(LIBBIN)/$@  $^

.PHONY : clean
clean:
	$(RM) $(OBJ) $(OBJC) ;


Could anyone please tell why it works in a terminal but not in eclipse ?
Thanks in advance for your help.
JM
  • Attachment: eclipse.png
    (Size: 150.09KB, Downloaded 26 times)
Re: Source codes that compile with command line but not in eclipse [message #1775763 is a reply to message #1775760] Mon, 06 November 2017 06:07 Go to previous messageGo to next message
David Vavra is currently offline David VavraFriend
Messages: 629
Registered: October 2012
Senior Member
The way 'make' works from the command line (without any options) is to
1) find the proper make file (there is a standard order of names to choose)
2) build the first target in that file.

In your case, the first target is "default"

The bult-in target Eclipse uses is "all" so it calls make with
cd <build configuration directory>
make all

This enters the configuration directory (the current configuration name often Debug or Release)
Then tells make to build target "all".

Either change the first target name from "default" to "all" or
Add a target "all" or
Add "default" as a target using the "Target View"
which you can add with Window --> Show View --> Build Targets
You will need to select and build this target every time.

Your makefile was not generated by Eclipse.
To see how Eclipse builds one, add the "Hello World" project and build it.
Then note the directory structure.
You don't have to use this structure (I don't for Makefile Projects) but
you still need to ensure Eclipse will try to set the directory containing the Makefile as the working directory during the build.
Project --> Properties --> C/C++ Build --> Builder Settings tab --> Build Directory
For you: ${workspace_loc:/test_code_makefile}

[Updated on: Mon, 06 November 2017 08:29]

Report message to a moderator

Re: Source codes that compile with command line but not in eclipse [message #1775775 is a reply to message #1775763] Mon, 06 November 2017 08:50 Go to previous messageGo to next message
J M is currently offline J MFriend
Messages: 7
Registered: November 2017
Junior Member
Hi David,

Thank you for your answer.
Well I'm not sure I understand fully what you said but I added and built the "Hello World" project and I noticed that it had the same directory structure as my project.
I changed the makefile and I added the "all" and after compilation, I had linking problem errors with libraries that say : "undefined reference towards the library ...." (you'll find attached an image with errors). I don't have these errors when I compile by command line. Here's my new makefile :
all: libImageFormats libBasic libImageFormatPM libImage libHyperspectral

OBJC = libImageFormats.o
OBJ	= libBasic.o libImageFormatPM.o libImage.o libHyperspectral.o

CXX=g++
C=gcc


hdrdir=   -I/usr/local/include/   #-I/usr/local/opt/libpng/include
libdir=    -L/usr/local/lib/      #-L/usr/local/opt/libpng/lib


COPT	= -lm -O3 -funroll-loops -fomit-frame-pointer -ffast-math -ftree-vectorize
CXXFLAGS  +=  -g $(COPT)   -pedantic -Wall -Wextra    -Wno-write-strings  -Wno-deprecated  $(hdrdir) -fopenmp
LDFLAGS += -g $(CXXFLAGS) $(libdir) -lpng -ltiff -ljpeg -lfftw3 -lfftw3f -fopenmp


CFLAGS =  -O3 -std=c99 -I/usr/local/include/   -L/usr/local/lib/


default: $(OBJC) $(OBJ)  $(BIN)


$(OBJC) : %.o : %.c
	$(C) -c $(CFLAGS)   $< -o $@


$(OBJ) : %.o : %.cpp
	$(CXX) -c $(CXXFLAGS)   $< -o $@


$(BIN) : % : %.o
	$(CXX) $(LDFLAGS)  -o $(LIBBIN)/$@  $^

.PHONY : clean
clean:
	$(RM) $(OBJ) $(OBJC) ;


Thank you in advance for you help.
Sincerely,
-J

[Updated on: Mon, 06 November 2017 09:13]

Report message to a moderator

Re: Source codes that compile with command line but not in eclipse [message #1775780 is a reply to message #1775775] Mon, 06 November 2017 09:45 Go to previous messageGo to next message
David Vavra is currently offline David VavraFriend
Messages: 629
Registered: October 2012
Senior Member
The directory structure I get with HelloWorld looks different than your project.
Here is mine:
index.php/fa/31230/0/
Note that makefile is in the Debug configuration directory.
But since you got some make output, you must be doing things right.

The problems you are getting now are external to Eclipse and fixing them is off topic for this forum.
If you type make all from the command line, you will get the same errors.
Eclipse isn't doing anything beyond what the shell would do when running make.

I think you really wanted target "all" the same as target "default"
Like: all: $(OBJC) $(OBJ) $(BIN)
And placed after "default"
You also seem to be missing the "main" function.
Also, library files have to appear after all references to them.
For example, you have libImageFormat.o after -ltiff in the command which is backwards.

As I said, this forum really isn't here to solve non-Eclipse problems.
I don't know your code and what you are really trying to do.
You may need to consult a tutorial or ask on forums such as stackoverflow.com for further resolution.

Re: Source codes that compile with command line but not in eclipse [message #1775799 is a reply to message #1775780] Mon, 06 November 2017 15:38 Go to previous messageGo to next message
J M is currently offline J MFriend
Messages: 7
Registered: November 2017
Junior Member
I changed many things and your answers were of big help. It's normal that our "Hello world" projects don't look alike because mine was created as a "Makefile project". Now everything compiles fine, the errors were due to the way the makefile is written under eclipse and here's my new makefile for people who might have the same problem:

#CXX	=	g++
#C	=	gcc


hdrdir=  	-I/usr/local/include/  	#-I/usr/local/opt/libpng/include
libdir=	-L/usr/local/lib/	#-L/usr/local/opt/libpng/lib


COPT	= -lm -O3 -funroll-loops -fomit-frame-pointer -ffast-math -ftree-vectorize
CXXFLAGS 	+= 	-g $(COPT)   -pedantic -Wall -Wextra    -Wno-write-strings  -Wno-deprecated  $(hdrdir) -fopenmp
LDFLAGS	+=	-g $(CXXFLAGS) $(libdir) -lpng -ltiff -ljpeg -lfftw3 -lfftw3f -fopenmp


CFLAGS	=	-O3 -std=c99 -I/usr/local/include/   -L/usr/local/lib/

OBJC	=	libImageFormats.o
OBJ	=	libBasic.o libImageFormatPM.o libImage.o libHyperspectral.o

all:	$(OBJC)	$(OBJ)	$(BIN)

$(OBJC) : %.o : %.c
	$(C) -c $(CFLAGS)   $< -o $@


$(OBJ) : %.o : %.cpp
	$(CXX) -c $(CXXFLAGS)   $< -o $@


$(BIN) : % : %.o
	$(CXX) $(LDFLAGS)  -o $(LIBBIN)/$@  $^

.PHONY : clean
clean:
	$(RM) $(OBJ) $(OBJC) ;


However, I tried to put my codes in a folder named library and, as I read in the eclipse guide page, I changed the path in :Project>>properties>>, I uncheck "use default build command" and in Build command I gave the path to the makefile that lives in the library folder now (see attached image : makefile_path.png ). When I build the project I have "no rule to make the target "libImageFormats.c" necessary for "libImageFormats.o", stop" error (see attached image : error_makefile_in_folder.png) and I don't know how to fix it. Do you have any ideas why it doesn't work even when I give the path of the makefile in the new folder ? And in case in the same project we have two folders with a makefile in each, how can the project be built ?

Your help is greatly appreciated.
Thanks in advance,
-J
Re: Source codes that compile with command line but not in eclipse [message #1775843 is a reply to message #1775799] Tue, 07 November 2017 07:54 Go to previous messageGo to next message
J M is currently offline J MFriend
Messages: 7
Registered: November 2017
Junior Member
Hello everyone,

Does anyone know how to fix the problem mentioned in my message above or should I create a new topic and re-ask the question ?

Thank you in advance.
-J
Re: Source codes that compile with command line but not in eclipse [message #1776001 is a reply to message #1775843] Thu, 09 November 2017 05:23 Go to previous message
David Vavra is currently offline David VavraFriend
Messages: 629
Registered: October 2012
Senior Member
My guess is that libImageFormats.c can't be found by make so it wants to build it.
Likely because you are executing make from the parent directory of the make file.

Trying to debug a makefile can be frustrating.
Fixing one is beyond the scope of this forum.
So you should direct your question to forums such as stackoverflow.com

You can also get tracing from make
and there is a macro to print things $(info: ...)
More at https://www.gnu.org/software/make/manual/html_node/index.html#SEC_Contents

You might want to create a simpler project using say just Hello.cpp and your makefile.
You could also try making a target "here" that prints the working directory (and maybe other stuff).
"echo" can be used in recipes.
Things might not be as you think they are

here:
        pwd


[Updated on: Thu, 09 November 2017 05:49]

Report message to a moderator

Previous Topic:Referenced projects building
Next Topic:Eclipse Hangs on Start: CDT Scanner Discovery
Goto Forum:
  


Current Time: Mon Dec 18 01:35:43 GMT 2017

Powered by FUDForum. Page generated in 0.02038 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software