I am trying to compile some C++ that depends on QuantLib, which in turn depends on Boost. I am working on a MacBook Pro M1 (ARM architecture). I installed Boost and QuantLib using the instructions on this page: https://www.quantlib.org/install/macosx.shtml. I then tried to compile the following source code:
#include <iostream>
#include <ql/quantlib.hpp>
int main(int, char*[])
{
QuantLib::Option::Type OptionType(QuantLib::Option::Call);
std::cout << "Option Type = " << OptionType << std::endl;
return 0;
}
using the following command:
clang++ -std=c++11 ql_ex1.cpp -o build/ql_ex1 $(INC) $(LIB) \
-I/opt/homebrew/Cellar/quantlib/1.23/include -I/opt/homebrew/Cellar/boost/1.76.0/include \
-L/opt/homebrew/Cellar/boost/1.76.0/lib -L/opt/homebrew/Cellar/quantlib/1.23/lib
This gave me the following error message:
Undefined symbols for architecture arm64:
"boost::assertion_failed(char const*, char const*, char const*, long)", referenced from:
long double boost::math::detail::sinpx<long double>(long double) in ql_ex1-044d3e.o
"boost::assertion_failed_msg(char const*, char const*, char const*, char const*, long)", referenced from:
boost::array<long double, 171ul>::operator[](unsigned long) const in ql_ex1-044d3e.o
...
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1
So far I have tried the following approaches,
Install x86 binaries and run clang++ in emulation. I see the same Undefined symbols error.
Try to identify a Boost shared library file that I need to link to. I have not been able to find anything that contains the assertion_failed symbol.
Can someone point me to where (if anywhere) this symbol is defined? It would be useful to know if I am just missing the right compiler options or whether there is a more fundamental issue that needs to be addressed.
I think you're missing a -lQuantLib in your command line? You're telling the compiler where to look for the libraries (the -L switches`) but not which libraries to link.
Also, check whether homebrew also installed a quantlib-config script on your computer. If so, it should provide the switches you need; try running
quantlib-config --cflags
which should output the -I flags specifying the location of the QuantLib headers (and possibly the -std=c++11 flag you're passing manually), and
quantlib-config --libs
which should output the corresponding -L switches as well as the -lQuantLib you missed.
If they work, you can use the command
clang++ ql_ex1.cpp -o build/ql_ex1 $(INC) $(LIB) \
-I/opt/homebrew/Cellar/boost/1.76.0/include \
`quantlib-config --cflags` `quantlib-config --libs`
and let the script fill in the info for you.
Related
I am trying to build a simple SSH client from Wil Allsopp's pen testing book. Working on Mac OS High Sierra with gcc-4.2 with libssh installed using Homebrew. The simplest version of the code is:
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
ssh_session my_ssh_session;
my_ssh_session = ssh_new();
ssh_free(my_ssh_session);
return 0;
}
However a simple gcc build (gcc -Wall ssh_client.c) produces the following error:
Undefined symbols for architecture x86_64:
"_ssh_free", referenced from:
_main in ssh_client-aa8f09.o
"_ssh_new", referenced from:
_main in ssh_client-aa8f09.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
Can anyone explain these errors and how I can fix them?
Your build line doesn't appear to contain any include and linker flags. I imagine it is picking up the headers but not the library for ssh to link against. Have a look into pkgconfig for an easy way to maintain this.
Since the recent update Mavericks, I have a problem linking with the library json_spirit.
I installed the new Command Line Tools, but I'm using gcc47 installed via macports. Json spirit (v4.06) has been compiled with the same gcc (after the update).
gcc (MacPorts gcc47 4.7.3_3) 4.7.3
I boiled it down to the following example :
test.cpp
#include <iostream>
#include "json_spirit_reader_template.h"
#include "json_spirit_writer_template.h"
#include "json_spirit_writer.h"
int main(int argc, char **argv) {
json_spirit::mValue a;
std::string s("[]");
json_spirit::read_string(s, a);
std::cout << json_spirit::write_formatted(a) << std::endl;
return 0;
}
which I compile with the following :
g++ -I/usr/local/include test.cpp -o test -L/usr/local/lib -ljson_spirit
And it gives this error :
Undefined symbols for architecture x86_64:
"json_spirit::write_formatted(json_spirit::Value_impl<json_spirit::Config_map<std::basic_strin g<char, std::char_traits<char>, std::allocator<char> > > > const&)", referenced from:
_main in ccwUclng.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
And finally, using "nm -g /usr/local/lib/libjson_spirit.a | grep write_formatted" :
nm: no name list
0000000000000150 T __ZN11json_spirit15write_formattedERKNS_10Value_implINS_10Config_mapINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEEE
000000000003afa8 S __ZN11json_spirit15write_formattedERKNS_10Value_implINS_10Config_mapINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEEE.eh
0000000000000120 T __ZN11json_spirit15write_formattedERKNS_10Value_implINS_10Config_mapINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEEERNS2_13basic_ostreamIcS5_EE
000000000003af80 S __ZN11json_spirit15write_formattedERKNS_10Value_implINS_10Config_mapINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEEERNS2_13basic_ostreamIcS5_EE.eh
0000000000000270 T __ZN11json_spirit15write_formattedERKNS_10Value_implINS_10Config_mapINSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEEEEEE
000000000003b118 S __ZN11json_spirit15write_formattedERKNS_10Value_implINS_10Config_mapINSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEEEEEE.eh
0000000000000240 T __ZN11json_spirit15write_formattedERKNS_10Value_implINS_10Config_mapINSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEEEEEERNS2_13basic_ostreamIwS5_EE
000000000003b0c8 S __ZN11json_spirit15write_formattedERKNS_10Value_implINS_10Config_mapINSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEEEEEERNS2_13basic_ostreamIwS5_EE.eh
0000000000000090 T __ZN11json_spirit15write_formattedERKNS_10Value_implINS_13Config_vectorINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEEE
000000000003aeb0 S __ZN11json_spirit15write_formattedERKNS_10Value_implINS_13Config_vectorINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEEE.eh
0000000000000060 T __ZN11json_spirit15write_formattedERKNS_10Value_implINS_13Config_vectorINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEEERNS2_13basic_ostreamIcS5_EE
000000000003ae88 S __ZN11json_spirit15write_formattedERKNS_10Value_implINS_13Config_vectorINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEEERNS2_13basic_ostreamIcS5_EE.eh
0000000000000210 T __ZN11json_spirit15write_formattedERKNS_10Value_implINS_13Config_vectorINSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEEEEEE
000000000003b0a0 S __ZN11json_spirit15write_formattedERKNS_10Value_implINS_13Config_vectorINSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEEEEEE.eh
00000000000001e0 T __ZN11json_spirit15write_formattedERKNS_10Value_implINS_13Config_vectorINSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEEEEEERNS2_13basic_ostreamIwS5_EE
000000000003b078 S __ZN11json_spirit15write_formattedERKNS_10Value_implINS_13Config_vectorINSt3__112basic_stringIwNS2_11char_traitsIwEENS2_9allocatorIwEEEEEEEERNS2_13basic_ostreamIwS5_EE.eh
I don't know how to read the nm output, so I may be missing something obvious.
I've tried to install and uninstall every component (boost ..) one by one, compiling with the gcc from macports and / or the default, which is :
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
Well, this is a half answer, because I never really got to explain exactly what happened, but I fixed it, so..
The answer was to rebuild boost from scratch, (icu as well) with the new system and the same compiler everywhere, for me it's gcc 4.7.3 installed with macports.
Also, I was trying to upgrade from boost 1.53 to boost 1.54, so it may have been part of the problem, but now anyway I went back to boost 1.53.
I have been using OpenCV for a while and also the imwrite function, but unfortunately this is not working any more.
I am running with OpenCV 2.4.3 with following sample code:
imwrite("somepath/somefile.png", myMat);
The error:
Undefined symbols for architecture x86_64:
"cv::imwrite(std::string const&, cv::_InputArray const&, std::__debug::vector<int, std::allocator<int> > const&)", referenced from:
MyProject::this_callback(int, void*) in MyProject.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The error looks somewhat familiar but i cannot determine whats wrong.
Yes, I've thought you were using XCode. I had the same problem. :)
If you change the project setup so that:
you use GNU++11 as C++ language dialect
libstdc++ (GNU C++ standard) as C++ standard library
your linking problem will go away.
I use Apple LLVM 4.1.
When I had this problem, I have tried just adding a new target to one of my old projects I knew, worked. Then I've just made that target a one-source-file program.
This must be a "magic" part of XCode as I think there was a time I could not get the same project working after a restart. :S
I did what Barnabas did and also got the issue on cvdef.h. I was getting desperate so and what I did was just changed the header file.
in /usr/local/cvdef.h line 205:
I changed
include <cstdint>
typedef std::uint32_t uint;
to:
include <tr1/cstdint>
typedef std::tr1::uint32_t uint;
based on this post
I think opencv devs should apply some changes to the code for 64bit..
use something like clang version 3.8.0 (trunk 257459), instead of gcc version 4.9.2 (MacPorts gcc49 4.9.2_1+universal) to compile
for reference, cmd: clang++ -std=c++11 `pkg-config --cflags --libs opencv` code.cpp -o code
OK, this is probably very basic, but I can't find the problem. I'm trying to link my C++ program with a C shared library (YAJL to name it) with -lyajl ; the linker seems to find it (it does not complain for that) but cannot find the symbols I use in my code :
$> g++ test.c -lyajl
Undefined symbols for architecture x86_64:
"yajl_tree_parse(char const*, char*, unsigned long)", referenced from:
_main in ccEqsAaJ.o
"yajl_tree_get(yajl_val_s*, char const**, yajl_type)", referenced from:
_main in ccEqsAaJ.o
"yajl_tree_free(yajl_val_s*)", referenced from:
_main in ccEqsAaJ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
$>
I made a minimalist test.c file to isolate the behavior. Well, what's very strange is that gcc seems perfectly OK with the code (as, incidentally, test.c is just C code, no C++ feature here):
$> gcc test.c -lyajl
$>
What is gcc doing that g++ doesn't seem to be able to do right ?
Could it be that the symbols in libyajl.so are C linkage, but your code (or the header file you are using) does not declare them extern "C"? In that case, compiling your code as C++ would result in name-mangled symbols in your object file, which would not match the (C linkage, unmangled) symbols in the library.
The solution would be to write, in the header file, something like this:
#ifdef __cplusplus
// "__cplusplus" is defined whenever it's a C++ compiler,
// not a C compiler, that is doing the compiling.
extern "C" {
#endif
// Exchange "void" with the real return type for these functions
void yajl_tree_parse(char const*, char*, unsigned long);
void yajl_tree_get(yajl_val_s*, char const**, yajl_type);
void yajl_tree_free(yajl_val_s*);
#ifdef __cplusplus
}
#endif
If extern "C" does not fix it, try this.
I had the same problem trying to link against the static library- libyajl_s.a.
It is possible to work around the problem by compiling the yajl source into the executable,
but I really needed the static library.
On closer inspection of the yajl source you will see that the source is looking for headers in two separate folders, e.g.
#include <api/yajl_tree.h>
#include <api/yajl_parse.h>
#include "yajl_parser.h"
The linker seems to resolve yajl_tree_parse, yajl_tree_get and yajl_tree_free to their prototypes in yajl_tree.h and not to their definitions in yajl_tree.c. In other words- they remain undefined in the resulting library.
The solution was to move all headers from the api folder into the src folder and remove all references to <api/...>. The linker then picked up the yajl_tree_xxx function definitions correctly.
I have just built and installed tiff-4.0.0beta6 on my Mac computer running Snow Leopard. I followed the tutorial at http://www.kyngchaos.com/macosx/build/libtiff. The install went fine but there are issues with the TIFF data type.
For exmaple, when I compile the following simple code:
#include "tiffio.h"
main()
{
TIFF* tif = TIFFOpen("foo.tif", "r");
TIFFClose(tif);
}
I get the error message:
hlrg-labs-imac:metrics Ben$ gcc main.c
Undefined symbols:
"_TIFFOpen", referenced from:
_main in cciewEwr.o
"_TIFFClose", referenced from:
_main in cciewEwr.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
When I compile the code:
#include "tiffio.h"
main()
{
TIFF tif;
}
I get the compilation error:
hlrg-labs-imac:metrics Ben$ gcc main.c
main.c: In function ‘main’:
main.c:5: error: storage size of ‘tif’ isn’t known
Any suggestions on this would be greatly appreciated.
Thanks.
When you compile you need to include the -ltiff switch. For example:
gcc main.c -ltiff -o main
Also, in your second example it should be
main(){ TIFF* tif; }