Call swift from c - 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

Related

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.

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

Boost logger linking issue

I am writing a program which uses Boost libraries. I had no problems with linking and using program options but I can't seem to make boost log working correctly.
Could anyone tell me what am I missing?
jamroot.jam
using clang : : : <compileflags>-Isrc/main/headers <compileflags>-std=c++11 <compileflags>-stdlib=libc++ <linkflags>-std=c++11 <linkflags>-stdlib=libc++ ;
lib boost_program_options boost_log ;
exe foghorn : [ glob src/main/cpp/*.cpp ] boost_program_options boost_log ;
This is the error I am getting:
"boost::log::v2s_mt_posix::record_view::public_data::destroy(boost::log::v2s_mt_posix::record_view::public_data const*)", referenced from:
boost::log::v2s_mt_posix::record::reset() in main.o
"boost::log::v2s_mt_posix::attribute_set::insert(boost::log::v2s_mt_posix::attribute_name, boost::log::v2s_mt_posix::attribute const&)", referenced from:
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>::add_attribute_unlocked(boost::log::v2s_mt_posix::attribute_name const&, boost::log::v2s_mt_posix::attribute const&) in main.o
boost::log::v2s_mt_posix::aux::attribute_set_reference_proxy::operator=(boost::log::v2s_mt_posix::attribute const&) const in main.o
"boost::log::v2s_mt_posix::attribute_set::attribute_set(boost::log::v2s_mt_posix::attribute_set const&)", referenced from:
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>::basic_logger(boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model> const&) in main.o
"boost::log::v2s_mt_posix::attribute_set::attribute_set()", referenced from:
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>::basic_logger() in main.o
"boost::log::v2s_mt_posix::attribute_set::~attribute_set()", referenced from:
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>::~basic_logger() in main.o
"boost::log::v2s_mt_posix::attribute_name::get_id_from_string(char const*)", referenced from:
boost::log::v2s_mt_posix::attribute_name::attribute_name(char const*) in main.o
"void boost::log::v2s_mt_posix::init_from_stream<char>(std::__1::basic_istream<char, std::__1::char_traits<char> >&)", referenced from:
__ZL13gvLoggingInitPKc in main.o
"boost::log::v2s_mt_posix::aux::this_thread::get_id()", referenced from:
boost::log::v2s_mt_posix::attributes::current_thread_id::impl::dispatch(boost::log::v2s_mt_posix::type_dispatcher&) in main.o
boost::log::v2s_mt_posix::attributes::current_thread_id::impl::detach_from_thread() in main.o
"boost::log::v2s_mt_posix::aux::this_process::get_id()", referenced from:
boost::log::v2s_mt_posix::attributes::current_process_id::current_process_id() in main.o
"boost::log::v2s_mt_posix::aux::stream_provider<char>::release_compound(boost::log::v2s_mt_posix::aux::stream_provider<char>::stream_compound*)", referenced from:
boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel> >::auto_release::~auto_release() in main.o
"boost::log::v2s_mt_posix::aux::stream_provider<char>::allocate_compound(boost::log::v2s_mt_posix::record&)", referenced from:
boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel> >::record_pump(boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>&, boost::log::v2s_mt_posix::record&) in main.o
"boost::log::v2s_mt_posix::aux::get_process_name()", referenced from:
boost::log::v2s_mt_posix::attributes::current_process_name::current_process_name() in main.o
"boost::log::v2s_mt_posix::aux::once_block_sentry::commit()", referenced from:
boost::log::v2s_mt_posix::aux::lazy_singleton<boost::log::v2s_mt_posix::sources::aux::logger_singleton<SLogger>, boost::shared_ptr<boost::log::v2s_mt_posix::sources::aux::logger_holder<boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel> > > >::get() in main.o
"boost::log::v2s_mt_posix::aux::once_block_sentry::rollback()", referenced from:
boost::log::v2s_mt_posix::aux::once_block_sentry::~once_block_sentry() in main.o
"boost::log::v2s_mt_posix::aux::default_attribute_names::severity()", referenced from:
boost::log::v2s_mt_posix::sources::basic_severity_logger<boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>, ESeverityLevel>::basic_severity_logger() in main.o
boost::log::v2s_mt_posix::sources::basic_severity_logger<boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>, ESeverityLevel>::basic_severity_logger(boost::log::v2s_mt_posix::sources::basic_severity_logger<boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>, ESeverityLevel> const&) in main.o
"boost::log::v2s_mt_posix::aux::unhandled_exception_count()", referenced from:
boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel> >::~record_pump() in main.o
boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel> >::record_pump(boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>&, boost::log::v2s_mt_posix::record&) in main.o
"boost::log::v2s_mt_posix::aux::attach_attribute_name_info(boost::exception&, boost::log::v2s_mt_posix::attribute_name const&)", referenced from:
boost::log::v2s_mt_posix::value_extractor<ESeverityLevel, boost::log::v2s_mt_posix::fallback_to_none, tag::severity>::operator()(boost::log::v2s_mt_posix::attribute_name const&, boost::log::v2s_mt_posix::attribute_value_set const&) const in main.o
"boost::log::v2s_mt_posix::core::set_filter(boost::log::v2s_mt_posix::filter const&)", referenced from:
__ZL13gvLoggingInitPKc in main.o
"boost::log::v2s_mt_posix::core::open_record(boost::log::v2s_mt_posix::attribute_set const&)", referenced from:
boost::log::v2s_mt_posix::record boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, ESeverityLevel const> >(boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, ESeverityLevel const> const&) in main.o
"boost::log::v2s_mt_posix::core::push_record_move(boost::log::v2s_mt_posix::record&)", referenced from:
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>::push_record_unlocked(boost::log::v2s_mt_posix::record&&) in main.o
"boost::log::v2s_mt_posix::core::add_global_attribute(boost::log::v2s_mt_posix::attribute_name const&, boost::log::v2s_mt_posix::attribute const&)", referenced from:
__ZL13gvLoggingInitPKc in main.o
"boost::log::v2s_mt_posix::core::get()", referenced from:
__ZL13gvLoggingInitPKc in main.o
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>::basic_logger() in main.o
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model>::basic_logger(boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<ESeverityLevel>, boost::log::v2s_mt_posix::sources::single_thread_model> const&) in main.o
"boost::log::v2s_mt_posix::sources::aux::global_storage::get_or_init(std::type_info const&, boost::shared_ptr<boost::log::v2s_mt_posix::sources::aux::logger_holder_base> (*)())", referenced from:
boost::log::v2s_mt_posix::sources::aux::logger_singleton<SLogger>::init_instance() in main.o
"boost::log::v2s_mt_posix::sources::aux::get_severity_level()", referenced from:
boost::log::v2s_mt_posix::sources::aux::severity_level<ESeverityLevel>::set_value(ESeverityLevel) in main.o
boost::log::v2s_mt_posix::sources::aux::severity_level<ESeverityLevel>::impl::dispatch(boost::log::v2s_mt_posix::type_dispatcher&) in main.o
boost::log::v2s_mt_posix::sources::aux::severity_level<ESeverityLevel>::impl::detach_from_thread() in main.o
"boost::log::v2s_mt_posix::sources::aux::throw_odr_violation(std::type_info const&, std::type_info const&, boost::log::v2s_mt_posix::sources::aux::logger_holder_base const&)", referenced from:
boost::log::v2s_mt_posix::sources::aux::logger_singleton<SLogger>::init_instance() in main.o
"boost::log::v2s_mt_posix::attribute::impl::operator delete(void*, unsigned long)", referenced from:
boost::log::v2s_mt_posix::sources::aux::severity_level<ESeverityLevel>::severity_level() in main.o
boost::log::v2s_mt_posix::sources::aux::severity_level<ESeverityLevel>::impl::~impl() in main.o
boost::log::v2s_mt_posix::sources::aux::severity_level<ESeverityLevel>::impl::detach_from_thread() in main.o
boost::log::v2s_mt_posix::attributes::attribute_value_impl<ESeverityLevel>::~attribute_value_impl() in main.o
boost::log::v2s_mt_posix::attribute_value::impl::~impl() in main.o
boost::log::v2s_mt_posix::attribute::impl::~impl() in main.o
boost::log::v2s_mt_posix::attributes::current_thread_id::current_thread_id() in main.o
...
"boost::log::v2s_mt_posix::attribute::impl::operator new(unsigned long)", referenced from:
boost::log::v2s_mt_posix::sources::aux::severity_level<ESeverityLevel>::severity_level() in main.o
boost::log::v2s_mt_posix::sources::aux::severity_level<ESeverityLevel>::impl::detach_from_thread() in main.o
boost::log::v2s_mt_posix::attributes::current_thread_id::current_thread_id() in main.o
boost::log::v2s_mt_posix::attributes::current_thread_id::impl::detach_from_thread() in main.o
boost::log::v2s_mt_posix::attributes::constant<boost::log::v2s_mt_posix::aux::id<boost::log::v2s_mt_posix::aux::process> >::constant(boost::log::v2s_mt_posix::aux::id<boost::log::v2s_mt_posix::aux::process>&&) in main.o
boost::log::v2s_mt_posix::attributes::constant<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::constant(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&) in main.o
boost::log::v2s_mt_posix::attributes::basic_clock<boost::log::v2s_mt_posix::attributes::local_time_traits>::basic_clock() in main.o
...
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in main.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.o
___cxx_global_var_init1 in main.o
"boost::log::v2s_mt_posix::attribute_value_set::end() const", referenced from:
boost::log::v2s_mt_posix::value_extractor<ESeverityLevel, boost::log::v2s_mt_posix::fallback_to_none, tag::severity>::operator()(boost::log::v2s_mt_posix::attribute_name const&, boost::log::v2s_mt_posix::attribute_value_set const&) const in main.o
"boost::log::v2s_mt_posix::attribute_value_set::find(boost::log::v2s_mt_posix::attribute_name) const", referenced from:
boost::log::v2s_mt_posix::value_extractor<ESeverityLevel, boost::log::v2s_mt_posix::fallback_to_none, tag::severity>::operator()(boost::log::v2s_mt_posix::attribute_name const&, boost::log::v2s_mt_posix::attribute_value_set const&) const in main.o
"boost::log::v2s_mt_posix::aux::once_block_sentry::enter_once_block() const", referenced from:
boost::log::v2s_mt_posix::aux::once_block_sentry::executed() const in main.o
"boost::log::v2s_mt_posix::core::get_logging_enabled() const", referenced from:
__ZN5boost3log12v2s_mt_posix7sources22basic_composite_loggerIcNS2_15severity_loggerI14ESeverityLevelEENS2_19single_thread_modelENS2_8featuresIJNS2_8severityIS5_EEEEEE11open_recordINS_9parameter3aux15tagged_argumentINS1_8keywords3tag8severityEKS5_EEEENS1_6recordERKT_ 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)
"clang++" -o "bin/clang-darwin-4.2.1/debug/foghorn" "bin/clang-darwin-4.2.1/debug/src/main/cpp/complement.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/degenerate.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/fastq.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/fastq_reader.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/interleave.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/interval_reader.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/interval_reader_test_tool.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/main.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/oligo_designer.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/reference_reader.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/reference_reader_test_tool.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/sequence_utils.o" "bin/clang-darwin-4.2.1/debug/src/main/cpp/tool.o" -lboost_log -lboost_program_options -g -std=c++11 -stdlib=libc++
...failed clang-darwin.link bin/clang-darwin-4.2.1/debug/foghorn...
...failed updating 1 target...
...updated 13 targets...
I had the same issue and in my case the problem was that I had boost compiled with dynamic libs only, and forgot to define BOOST_ALL_DYN_LINK.
http://www.boost.org/doc/libs/1_54_0/libs/log/doc/html/log/rationale/namespace_mangling.html
As a test, try to compile
http://www.boost.org/doc/libs/1_54_0/libs/log/example/doc/tutorial_fmt_stream.cpp
(#include <boost/log/support/date_time.hpp> should be added, the example is broken.)
and also add #define BOOST_LOG_DYN_LINK
I could compile the example with g++ tutorial_fmt_stream.cpp -Wall -L$BOOST/lib/ -I $BOOST/include/ -pthread -lboost_system -lboost_log_setup -lboost_log -lboost_date_time -lboost_thread -lrt -lboost_filesystem
If you are using cmake to manage your boost, just don't forget to have something like:
add_definitions(-DBOOST_LOG_DYN_LINK)
in your CMakeLists.txt
I had the same problem, which was a complicated linkage path. The message shows that is trying to link with the static libraries ( ....v2s ...), but the linker is only giving it dynamic libraries (because it is stupid).
Your build of Boost might be generating both .so and .a files, and gcc gets confused when both are present.
I had a library built with CMake that uses static Boost libraries. If you link this library to a program using a Makefile flow, the program needs to also specify the same static Boost libraries. However, the -l option for the linker is vague and not your friend here. If you built Boost to have both static (.a) and dynamic (.so) outputs, then the linker will chose the dynamic (.so) files first if you just use the -l option.
The workaround is to not use the -L and -l options to find the Boost static libraries (since it will fail), but to specify the full paths to the Boost libraries. For example,
gcc .... -L/opt/boost_1.55/lib -lboost_log
change to
gcc .... /opt/boost_1.55/lib/libboost_log.a
Another option is to specify the word -static to gcc, but this requires all libraries to be static, which probably won't work if you use anything besides boost (say the librt.so library).
If you are using
void boost::log::v2s_mt_posix::init_from_stream<char>(std::basic_istream<char, std::char_traits<char> >&)
function, then you need to link
libboost_log_setup.a

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

GCC linker can't find standard library?

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.