4

Yak-shaving alert.

Although I am precluded from displaying any source code, I figure with a well-written post I may be able to provide enough info to get assistance. The steps I have tried below have all been garnered from other posts, and it's becoming a bit circular now.

I'm on OS X with the following:

MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ which g++
/usr/bin/g++

MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ arch
i386

MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr     
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr     
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ clang++ --version
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ brew --config
HOMEBREW_VERSION: 0.9.5
ORIGIN: https://github.com/Homebrew/homebrew
HEAD: edcf1d119c4ca9d79d7147a684b7d74767cbb1f6
Last commit: 6 weeks ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_REPOSITORY: /usr/local
HOMEBREW_CELLAR: /usr/local/Cellar
HOMEBREW_BOTTLE_DOMAIN: https://homebrew.bintray.com
CPU: dual-core 64-bit penryn
OS X: 10.9.5-x86_64
Xcode: N/A
CLT: 6.2.0.0.1.1424975374
Clang: 6.0 build 600
X11: 2.7.7 => /opt/X11
System Ruby: 2.0.0-p481
Perl: /usr/bin/perl
Python: /Library/Frameworks/Python.framework/Versions/2.7/bin/python => /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Ruby: /usr/bin/ruby => /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
Java: 1.6.0_65-b14-468

So I am given three files:

  • Metaphone3.cpp
  • Metaphone3ExampleCode.cpp
  • Metaphone3.h

I try to compile with g++:

g++ Metaphone3.cpp

I get:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • gcc and clang++ report the same.
  • Adding -m32 has no affect.
  • g++ Metaphone3.cpp -I /usr/local/include has no effect

If I try:

g++ -Wall -c Metaphone3.cpp

This gets rid of the warning, and a Metaphone3.o and Metaphone get generated.

If I try to execute:

MacBook-Pro-de-Pyderman:Metaphone3 Pyderman$ ./Metaphone
-bash: ./Metaphone: Malformed Mach-o file

Some more research indicates that I may have missed a linking step. So:

gcc Metaphone3.o -o Metaphone3

But this brings me back to the original error.

Other posts then suggest dropping the -c flag, but it is this very flag that enabled me to get passed the error. So you can see how this is getting circular. As you may be gathering by now, I am a developer, but not a C++ developer, and coming from Python, the compilation world is a new one to me. Any and all assistance appreciated

Community
  • 1
  • 1
Pyderman
  • 14,809
  • 13
  • 61
  • 106
  • 1
    `g++ -Wall -c Metaphone3.cpp` should only be generating `Metaphone.o` and not anything else – M.M Nov 03 '15 at 20:06

3 Answers3

2

An educated wild guess: main is in Metaphone3ExampleCode.cpp. You need to compile both, and link the resulting objects together.

Try

    g++ -c Metaphone3.cpp
    g++ -c Metaphone3ExampleCode.cpp
    g++ -o Methaphone Metaphone3.o Metaphone3ExampleCode.o

or

    g++ -o Methaphone Metaphone3.cpp Metaphone3ExampleCode.cpp
user58697
  • 7,808
  • 1
  • 14
  • 28
  • 1
    @JohanLundberg Most likely because it is a "library" type of code, providing utility functions. It is up to the user (i.e. you) to make an application using them. That's what the ExampleCode is for: to show you an example application. – user58697 Nov 03 '15 at 20:03
  • 1
    @JohanLundberg Metaphone3.cpp probably *isn't* referencing main. However all C++ programs must have a `main`. When you attempt to compile an executable, the compiler will insert startup code that initializes the environment and then calls your `main` function. If you did not provide a main function then you get the error you are seeing. – M.M Nov 03 '15 at 20:05
2

metaphone3.cpp should be compiled to an .so - it's a library and not an application

the example code is provided as a guide and is not intended to be compiled

if you make metaphone3.so, you'll need to make a c++ application yourself to link to it and test it

0

In case anyone else stumbles upon this in a google search, I received the same error above because of a simple typo: maint() instead of main()

smh