1

I'm having a bit of trouble trying to compile the following simple code in cygwin:

main.cpp:

#include <iostream>
#include "Point.h"

using namespace std;

int main() {
    Point a;
    return 0;
}

Point.cpp:

#include <iostream>
#include "Point.h"

using namespace std;

Point::Point() {
    cout << "Compile test" << endl;
}

Point.h:

#ifndef POINT_H
#define POINT_H

class Point {
    public:
        Point();
};

#endif

Make file:

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=main.cpp Point.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=test

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

%.o : %.cpp
    $(CC) $(CFLAGS) -c $<

clean:
    rm -rf *.o core

When I try to compile main.cpp, I'm getting the following error:

main.o:main.cpp:(.text+0x15): undefined reference to `Point::Point()'
main.o:main.cpp:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Point::Point()'

And when I try to compile Point.cpp, I'm getting:

/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-2.2.1-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-2.2.1-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'

All files are in the same directory. I should also note that the main class compiles perfectly fine if I don't create the point object. Any ideas? Would really appreciate some help, thanks!

Lachie
  • 105
  • 1
  • 11
  • You aren't declaring a type for Point::Point – stark Aug 31 '15 at 01:05
  • 2
    @stark It is a constructor, it does not have a return type. – Baum mit Augen Aug 31 '15 at 01:43
  • 1
    one thing you could do is, instead of showing us the make file, show us what make outputs when you do it with the "verbose" option so we see the actual command lines. – Chris Beck Aug 31 '15 at 03:25
  • @ChrisBeck Sorry but I have no idea what verbose is, can you explain how I use it and I'll give that a try. I didn't write the makefile myself by the way, was just provided by my lecturer. – Lachie Aug 31 '15 at 03:51
  • 1
    so my assumption is that your build system is messed up, and you aren't actually compiling `Point.cpp`. You can check if you are or not by looking at what gets printed in the console when you use `make VERBOSE=1`, more info here: http://stackoverflow.com/questions/5820303/how-do-i-force-make-gcc-to-show-me-the-commands – Chris Beck Aug 31 '15 at 03:55
  • @ChrisBeck Ahh thanks. So I've tried `make VERBOSE=1`, `make -n`, `make V=1`, `make --dry-run` and they all just output: `make: 'all' is up to date.` – Lachie Aug 31 '15 at 04:06
  • @ChrisBeck It seems that if I clean all the .o files, try compile make and Point, and then run the `make VERBOSE=1`, I get a different output: `g++ main.o Point.o -o test` – Lachie Aug 31 '15 at 04:08
  • 1
    Nevermind, I got it to compile! I just had to change `EXECUTABLE=test` in the makefile to `EXECUTABLE=main`. Thanks for the help chris, wouldn't have solved it without you! – Lachie Aug 31 '15 at 04:11

2 Answers2

1

(Reposting from comments since this turned out to solve it)

So, my assumption is that your build system is messed up, and you aren't actually compiling Point.cpp.

You can check if you are or not by looking at what gets printed in the console when you use make VERBOSE=1, more info here:

How do I force make/gcc to show me the commands?

Community
  • 1
  • 1
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
0

I get the same undefined reference to `WinMain' problem when I forget the executable name for the linking.

g++ -o source1.o source2.o

instead of

g++ -o executable source1.o source2.o

I did not find the answer anywhere for my problem so I hope it helps...