GCC linker can't find standard library? - c++

I've been developing a school project in XCode. The final product has to be submitted in source code with a makefile, so I wrote a makefile and to start compiling that way, to make sure I had a working copy. Here's my makefile:
all: main.o StackList.o world.o Farm.o
gcc main.o StackList.o world.o Farm.o -g -o Project1
main.o:
gcc -g -c main.cpp
StackList.o:
gcc -g -c Stacklist.cpp
world.cpp:
gcc -g -c world.cpp
Farm.cpp:
gcc -g -c Farm.cpp
clean:
rm *.o Project1
Compiling each of the object files works fine, but when it gets to "all," the linking step, it appears to not be aware of the standard library. I get "undefined symbols" error for everythin from "cin", to "basic_string", to "operator new".
I was under the impression that these things didn't need to be indicated directly, and in fact have not needed to do so in the past.
Any idea what might be happening?
EDIT:
If it helps, here's the start of the (very long) error message:
Undefined symbols for architecture x86_64:
"std::cin", referenced from:
_main in main.o
"std::cout", referenced from:
_main in main.o
Farm::print(int) in Farm.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
_main in main.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in main.o
__static_initialization_and_destruction_0(int, int)in StackList.o
__static_initialization_and_destruction_0(int, int)in world.o
__static_initialization_and_destruction_0(int, int)in Farm.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in main.o
___tcf_0 in StackList.o
___tcf_0 in world.o
___tcf_0 in Farm.o
"operator new(unsigned long)", referenced from:
doStackSearch(std::basic_istream<char, std::char_traits<char> >*, std::list<Farm*, std::allocator<Farm*> >*&)in world.o

To link C++ code, you need to use the g++ command, not the gcc command.
When compiling, the gcc command knows that it's compiling C++ code because of the .cpp suffix (but it's a good idea to use the g++ command anyway).
When linking, the gcc command just sees a bunch of .o files. The g++ command differs from the gcc command in that it knows where to find the C++-specific libraries.
(The fact that the name gcc refers both to the command, which is usually used to compile C code, and the "GNU Compiler Collection" as a whole, can be a little confusing.)

You need to use g++ to compile and link C++ source code, not gcc.
Also, you can leave out all targets besides the first and last ones. make knows how to compile .cpp files to .o already -- you don't have to tell it how.

I know this is old, but if you are compiling and linking C++, you can specify the standard library yourself. Add -lstdc++ at end of your command.

gcc main.o StackList.o world.o Farm.o -g -o Project1
What in this command line do you think tells gcc to link in the C++ standard library? There are no C++ files being linked. You haven't specified a language. And you invoke the compilter as gcc.

Related

Call swift from c

I saw someone use swift code from android, using #_cdecl swift functions. I am able to compile to native using swiftc -emit-object, but when trying to link, I cant get the linker to work correctly..
I am trying to use g++ or clang(++) to compile and link a native swift object, has someone successfully done this already?
The errors I get are listed below:
Undefined symbols for architecture x86_64:
"__T0S2SBp21_builtinStringLiteral_Bw17utf8CodeUnitCountBi1_7isASCIItcfC", referenced from:
__T04main4testyyF in main.o
"__T0SSN", referenced from:
__T04main4testyyF in main.o
"__T0s27_allocateUninitializedArraySayxG_BptBwlFyp_Tgq5", referenced from:
__T04main4testyyF in main.o
"__T0s5printySayypGd_SS9separatorSS10terminatortF", referenced from:
__T04main4testyyF in main.o
"__T0s5printySayypGd_SS9separatorSS10terminatortFfA0_", referenced from:
__T04main4testyyF in main.o
"__T0s5printySayypGd_SS9separatorSS10terminatortFfA1_", referenced from:
__T04main4testyyF in main.o
"_swift_bridgeObjectRelease", referenced from:
__T04main4testyyF in main.o
"_swift_bridgeObjectRetain", referenced from:
__T04main4testyyF in main.o
So basically I have two questions, can it be done and how?
John.
So thanx to amine.ahd's pointer, I had the iphoneos toolchain location linked instead of the mac osx's.
The following command actually compiled and let me run a c program that calls a swift native library, even with parameters (char* on the c side, UnsafePointer on the swift side).
clang -o m m.o -lshared -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -L.
*-lshared is the libshared.so library that is compiled using the following command:
clang -shared -o libshared.so main.o -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx
Notice the main.o, this got produced using the following command:
swiftc main.swift -emit-object
Thanx for the hints

Compiling & linking a Swift/Objective-C++ app via the command line

After adding a C++ component to my Swift application that is being compiled and run via the command line, I now need to compile the C++ and Objective-C++ (*.mm) files and link them with the Swift application.
Makefile:
all: foo-renderer
clean:
rm -f foo-renderer cpp.o objc.o
cpp.o: FooRenderer/FooRenderer/FooLibrary.cpp
clang++ -c -o $# $^
objc.o: cpp.o FooRenderer/FooRenderer/FooLibraryWrapper.mm
clang++ -c -framework Foundation -o $# $^
foo-renderer: objc.o FooRenderer/FooRenderer/*.swift
xcrun -sdk macosx swiftc -import-objc-header FooRenderer/FooRenderer/FooRenderer-Bridging-Header.h -o $# $^
The compiler fails with these errors:
ld: warning: object file (objc.o) was built for newer OSX version (10.12) than being linked (10.9)
Undefined symbols for architecture x86_64:
"__ZN11FooLibrary23printifyERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE", referenced from:
-[FooLibraryWrapper printify:] in objc.o
"__ZN11FooLibrary8optimizeERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE", referenced from:
-[FooLibraryWrapper optimize:] in objc.o
"__ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm", referenced from:
-[FooLibraryWrapper printify:] in objc.o
-[FooLibraryWrapper optimize:] in objc.o
"__ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev", referenced from:
-[FooLibraryWrapper printify:] in objc.o
-[FooLibraryWrapper optimize:] in objc.o
"__ZSt9terminatev", referenced from:
___clang_call_terminate in objc.o
"___cxa_begin_catch", referenced from:
___clang_call_terminate in objc.o
"___gxx_personality_v0", referenced from:
-[FooLibraryWrapper printify:] in objc.o
-[FooLibraryWrapper optimize:] in objc.o
Dwarf Exception Unwind Info (__eh_frame) in objc.o
ld: symbol(s) not found for architecture x86_64
My first instinct was that the standard C++ libraries weren't being included. I tried adding these lib flags to the following command, but to no avail:
cpp.o: FooRenderer/FooRenderer/FooLibrary.cpp
clang++ -c --std=c++14 -lstdc++ -lc++ -stdlib=libstdc++ -o $# $^
What am I missing?
One problem is that cpp.o is missing from the last command line. Adding it to the dependency list should do the trick:
foo-renderer: objc.o FooRenderer/FooRenderer/*.swift cpp.o
xcrun -sdk macosx swiftc -import-objc-header FooRenderer/FooRenderer/FooRenderer-Bridging-Header.h -o $# $^
That's because the linker needs the definitions of the symbols found in the C++ code.
Adding the -l... flags to the command for the cpp.o target is unnecessary because those are linker flags, and at that point you are just compiling.
Also, you likely don't need the cpp.o dependency for the objc.o target, since you are still compiling and compilation doesn't depend on other object files. You could probably do OK without -framework Foundation, but it doesn't hurt anything.
Though I am not very familiar with objc/clang/swift, but i have some pointers for you on how to proceed,
> "__ZN11FooLibrary23printifyERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE",
> referenced from:
> -[FooLibraryWrapper printify:] in objc.o
The above says that in objc object file in function printify, you have an undefined symbol, which could be FooLibrary::printify::basic_string::char_traits, so most probably you are missing a c++ library, try adding libc++ library in your linking command.
cpp.o: FooRenderer/FooRenderer/FooLibrary.cpp
clang++ -c --std=c++14 -lstdc++ -lc++ -stdlib=libstdc++ -o $# $^
And there is no meaning to add linker flags/libraries during compile time.

POCO Linking error, undefined symbols for architecture x86_64 on OS X 10.11.2

I installed POCO using Homebrew.
Someone suggested using stdlibc++ instead, but when I do that, I get a bunch of std linking errors in addition to the POCO linking errors.
I also tried using g++ from Homebrew's gcc instead of Apple's g++, but that gives the same errors.
Makefile
CXX=g++
RM=rm -f
CXXFLAGS=-std=c++11 -Wall -O3 -I/usr/local/opt/openssl/include -I/usr/local/Cellar/poco/1.6.1/include
LDFLAGS=-L/usr/local/opt/openssl/lib -L/usr/local/Cellar/poco/1.6.1/lib
LDLIBS=-lPocoNet -lPocoFoundation
SRCS=eml.cc
OBJS=$(subst .cc,.o,$(SRCS))
all: eml
eml: $(OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o eml $(OBJS) $(LDLIBS)
%.o: %.c
$(CXX) $(CXXFLAGS) -c $< -o $#
clean:
$(RM) eml $(OBJS)
Errors
g++ -std=c++11 -stdlib=libc++ -Wall -O3 -I/usr/local/opt/openssl/include -I/usr/local/Cellar/poco/1.6.1/include -L/usr/local/opt/openssl/lib -L/usr/local/Cellar/poco/1.6.1/lib -o eml eml.o -lPocoNet -lPocoFoundation -lcrypto
Undefined symbols for architecture x86_64:
"Poco::Net::SSLManager::initializeClient(Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler, Poco::ReferenceCounter, Poco::ReleasePolicy<Poco::Net::PrivateKeyPassphraseHandler> >, Poco::SharedPtr<Poco::Net::InvalidCertificateHandler, Poco::ReferenceCounter, Poco::ReleasePolicy<Poco::Net::InvalidCertificateHandler> >, Poco::AutoPtr<Poco::Net::Context>)", referenced from:
_main in eml.o
"Poco::Net::SSLManager::instance()", referenced from:
_main in eml.o
"Poco::Net::initializeSSL()", referenced from:
_main in eml.o
"Poco::Net::uninitializeSSL()", referenced from:
_main in eml.o
"Poco::Net::SecureStreamSocket::SecureStreamSocket(Poco::Net::SocketAddress const&)", referenced from:
_main in eml.o
"Poco::Net::SecureStreamSocket::~SecureStreamSocket()", referenced from:
_main in eml.o
"Poco::Net::AcceptCertificateHandler::AcceptCertificateHandler(bool)", referenced from:
_main in eml.o
"Poco::Net::Context::Context(Poco::Net::Context::Usage, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, Poco::Net::Context::VerificationMode, int, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
_main in eml.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [eml] Error 1
You are not linking all the required libraries. All of the unresolved
symbols are defined in libPocoNetSSL. To install this library you must build the
full version, not the basic version version, of the POCO libraries.
If you consult the documentation of any of the POCO APIs it will tell you the libraries you need to link,
e.g. Poco::Net::SSLManager::initializeClient.

linking C++ programs to Python

For linking C++ and python I used the following commands:
Compiling the actual function:
g++ -c -fpic main.cpp
Generate the wrapping C code using SWIG:
swig -c++ -python main.i
Compile the wrapping C code:
g++ -c -fpic main_wrap.cxx -I/usr/include/python2.7
for Linking everything together I used these tow instruction:
1) g++ -shared main.o main_wrap.o -o _main.so
2) g++ -lpython -shared main.o main_wrap.o -o _main.so
and got errors in both but less in the second one. (for the func.c example I checked and it only be linked with -lpython flag be set!)
however, it still gives me following errors:
Undefined symbols for architecture x86_64:
"std::vector<double, std::allocator<double> > power_method<double>(double, int)", referenced from:
__wrap_power_methodDouble in main_wrap.o
"std::vector<float, std::allocator<float> > power_method<float>(float, int)", referenced from:
__wrap_power_methodFloat in main_wrap.o
"std::vector<int, std::allocator<int> > power_method<int>(int, int)", referenced from:
__wrap_power_methodInt in main_wrap.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Show message history

Error building simple Qt5 application

I installed Qt 5.0.0 (qt-mac-opensource-5.0.0-clang-offline.dmg) on a Mac OS X 10.7.5 and now I'm trying to compile a "Hello World" application using Qt Creator 2.6.1.
The build process complains about a directory not found: /Users/karlphillip/Qt5.0.0/5.0.0/clang_64/qtbase/lib , followed by several linking errors:
clang++ -c -pipe -mmacosx-version-min=10.6 -O2 -Wall -W -fPIE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/karlphillip/Qt5.0.0/5.0.0/clang_64/mkspecs/macx-clang -I. -I/Users/karlphillip/Qt5.0.0/5.0.0/clang_64/include -I/Users/karlphillip/Qt5.0.0/5.0.0/clang_64/include/QtGui -I/Users/karlphillip/Qt5.0.0/5.0.0/clang_64/lib/QtGui.framework/Versions/5/Headers -I/Users/karlphillip/Qt5.0.0/5.0.0/clang_64/include/QtCore -I/Users/karlphillip/Qt5.0.0/5.0.0/clang_64/lib/QtCore.framework/Versions/5/Headers -I. -I/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/System/Library/Frameworks/AGL.framework/Headers -o main.o main.cpp
clang++ -headerpad_max_install_names -mmacosx-version-min=10.6 -o hello_qt.app/Contents/MacOS/hello_qt main.o -F/Users/karlphillip/Qt5.0.0/5.0.0/clang_64/lib -framework QtGui -F/Users/karlphillip/Qt5.0.0/5.0.0/clang_64/qtbase/lib -framework QtCore -framework OpenGL -framework AGL
ld: warning: directory not found for option '-F/Users/karlphillip/Qt5.0.0/5.0.0/clang_64/qtbase/lib'
Undefined symbols for architecture x86_64:
"QApplication::QApplication(int&, char**, int)", referenced from:
_main in main.o
"QLabel::QLabel(QString const&, QWidget*, QFlags<Qt::WindowType>)", referenced from:
_main in main.o
"QWidget::show()", referenced from:
_main in main.o
"QPushButton::QPushButton(QString const&, QWidget*)", referenced from:
_main in main.o
"QApplication::exec()", referenced from:
_main in main.o
"QPushButton::~QPushButton()", referenced from:
_main in main.o
"QLabel::~QLabel()", referenced from:
_main in main.o
"QApplication::~QApplication()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello_qt.app/Contents/MacOS/hello_qt] Error 1
It's important to note that the path /Users/karlphillip/Qt5.0.0/5.0.0/clang_64/qtbase/lib is not valid because the directory qtbase doesn't exist. The working path is /Users/karlphillip/Qt5.0.0/5.0.0/clang_64/lib. I'm not referring to qtbase anywhere in my project, so this is probably being added by qmake.
As it turns out, the invalid qtbase path thing is really a Qt bug originally reported at QTBUG-28336.
The linking problem is due to my project not linking with QtWidgets, which is something we are going to have to do starting in Qt5. This is accomplished by adding the following line to your .pro file:
QT += widgets
i had the same problem and to be honest im not shure how i managed it to solve.
i think/guess i copied the needed folders from the sources into the matching 'clang_64' folders and that does the job.
soo long zai