Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » linker error: can't find _main
linker error: can't find _main [message #644175] Thu, 09 December 2010 13:30 Go to next message
Eclipse UserFriend
Hi, all

I'm not sure this is an eclipse problem per se, but in my current project (a very simple Qt integration), I'm getting the following error:

Quote:
_main in hello.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [hello] Error 1



I've created several other projects that built without incident. The only differences I can see about this one is:

- I'm including some Qt files
- main is getting passed argv and argc

In fact, here's the entirety of the code:

#include <QApplication>
#include <QPushButton>

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);

     QPushButton hello("Hello world!");
     hello.resize(100, 30);

     hello.show();
     return app.exec();
 }


Thanks for any help.
Re: linker error: can't find _main [message #644423 is a reply to message #644175] Fri, 10 December 2010 23:36 Go to previous messageGo to next message
Eclipse UserFriend
Any ideas? I'm kind of stuck on this one. Thanks...
Re: linker error: can't find _main [message #647829 is a reply to message #644423] Sun, 09 January 2011 12:41 Go to previous messageGo to next message
Eclipse UserFriend
Can you include your build output from the console? I remember having a similar problem with main and it had to do with the defines. This has more to do with Qt than CDT though.
Re: linker error: can't find _main [message #647847 is a reply to message #647829] Sun, 09 January 2011 21:28 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for responding. Here's the output from the console:

**** Build of configuration Debug for project hello ****

make all 
Building file: ../src/hello.cpp
Invoking: GCC C++ Compiler
g++ -I/Library/Frameworks/QtGui.framework/Versions/4/Headers -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/hello.d" -MT"src/hello.d" -o"src/hello.o" "../src/hello.cpp"
Finished building: ../src/hello.cpp
 
Building target: hello
Invoking: MacOS X C++ Linker
g++  -o "hello"  ./src/hello.o   
Undefined symbols:
  "QString::free(QString::Data*)", referenced from:
      QString::~QString()in hello.o
  "QPushButton::QPushButton(QString const&, QWidget*)", referenced from:
      _main in hello.o
  "QWidget::resize(QSize const&)", referenced from:
      QWidget::resize(int, int)in hello.o
  "QString::fromAscii_helper(char const*, int)", referenced from:
      QString::QString(char const*)in hello.o
  "QPushButton::~QPushButton()", referenced from:
      _main in hello.o
      _main in hello.o
  "QApplication::QApplication(int&, char**, int)", referenced from:
      _main in hello.o
  "QApplication::exec()", referenced from:
      _main in hello.o
  "QApplication::~QApplication()", referenced from:
      _main in hello.o
      _main in hello.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [hello] Error 1
Re: linker error: can't find _main [message #647939 is a reply to message #647847] Mon, 10 January 2011 09:40 Go to previous messageGo to next message
Eclipse UserFriend
In your project properties, C/C++ Build, Settings, Tool Settings, MacOS X Linker, Miscellaneous

Add this to Linker flags:
-framework QtCore -framework QtGui
Re: linker error: can't find _main [message #647981 is a reply to message #647939] Mon, 10 January 2011 11:48 Go to previous messageGo to next message
Eclipse UserFriend
Marc-Andre -

Thank you so much. That seems to have worked.

May I ask how you were able to determine that solution from the console information I provided? Are those flags universal for Qt programs?

Thanks again.

Re: linker error: can't find _main [message #648001 is a reply to message #647981] Mon, 10 January 2011 12:49 Go to previous messageGo to next message
Eclipse UserFriend
I noticed in the command that Qt was not being linked, which is done with -framework Foo (Mac only) or -lfoo or libfoo.a. Hence the undefined symbols. I don't know how familiar you are with linking vs compiling... If you get undefined symbols for external libraries, it usually means that you are not linking the libraries. On Mac this is often done with the -framework flag.

By default, the linker will look for frameworks in those two directories:
/Library/Frameworks
/System/Library/Frameworks

For examble, I saw QString::free was not defined so I looked for it and found a header:
/Library/Frameworks/QtCore.framework/Headers/QString
which means QtCore probably needs to be linked! So I added -framework QtCore

I have an extra tip for you using CDT on Mac with frameworks. Currently, CDT doesn't play well frameworks so you don't get nice code completion and navigation for code in frameworks.

See https://bugs.eclipse.org/bugs/show_bug.cgi?id=69529

BUT, you can workaround that by creating symbolic links which will make QtCore.framework/Headers/QString look like QtCore/QString to CDT

Here's what I do
cd /Users/mark/Documents/dev/cpp/include
ln -s /Library/Frameworks/QtCore.framework/Headers QtCore
ln -s /Library/Frameworks/QtGui.framework/Headers QtGui
etc...

Then in Eclipse, in my project properties, C/C++ General, Paths and symbols, in the Includes tab, Add..., File system..., I browse for /Users/mark/Documents/dev/cpp/include, Ok. BUT sometimes Qt stuff gets included without the framwork path, like #include<QPushButton> instead of #include <QtGui/QPushButton> so I add those includes paths as well:

/Users/mark/Documents/dev/cpp/include/QtGui
/Users/mark/Documents/dev/cpp/include/QtCore

So for your small project, I ended up with 3 include paths

/Users/mark/Documents/dev/cpp/include
/Users/mark/Documents/dev/cpp/include/QtGui
/Users/mark/Documents/dev/cpp/include/QtCore

and my build command looked like this:
g++ -I/Users/mark/Documents/dev/cpp/include -I/Users/mark/Documents/dev/cpp/include/QtCore -I/Users/mark/Documents/dev/cpp/include/QtGui -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/testQt.d" -MT"src/testQt.d" -o "src/testQt.o" "../src/testQt.cpp
then
g++ -framework QtCore -framework QtGui -o "testQt" ./src/testQt.o

And if I type hello.[ctrl+space] I get all the sweet completion proposals!

Hope this helps!

[Updated on: Mon, 10 January 2011 16:12] by Moderator

Re: linker error: can't find _main [message #648176 is a reply to message #648001] Tue, 11 January 2011 11:05 Go to previous messageGo to next message
Eclipse UserFriend
Hi, Marc-Andre

Thank you very much for the detailed reply. I do have a few follow up questions:

1. I am familiar with linking, but not so familiar with UNIX/Mac development terms. Is "framework" simply the Mac name for a run-time library?

2. The console output seems to imply that g++ is the command for both compilation and linking. Is this true?

3. Instead of using linker flags, could the frameworks be added to the list of linker libraries in the properties area?

And, thanks for the workaround on the frameworks...I'll give that a try soon.
Re: linker error: can't find _main [message #690688 is a reply to message #648001] Wed, 29 June 2011 22:40 Go to previous message
Eclipse UserFriend
Marc-Andre,

I included a few frameworks includes as you suggested, and two things happened.

1) the compilation time increased by a factor of magnitude.

2) the included frameworks require other frameworks, which produce a lot of errors.

How do you cope with these issues?

Thanks.

--8
Previous Topic:Make Target in Indigo
Next Topic:"Can't run program 'gcc'" && "Binary not found."
Goto Forum:
  


Current Time: Tue Jul 29 11:18:08 EDT 2025

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

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

Back to the top