I'm using cmake to build my c++-project that uses a library that is located in my "/usr/local/bin/" directory.
The relevant part in the CMakeList.txt reads:
CHECK_INCLUDE_FILES("/usr/local/include/fann.h" HAVE_FANN_HEADER)
CHECK_LIBRARY_EXISTS(fann fann_get_errno "/usr/local/lib/" HAVE_FANN_LIB)
if(${HAVE_FANN_HEADER} AND ${HAVE_FANN_LIB})
the header is found without a problem the while library is not. Looking into the CMakeError.txt shows:
`/usr/bin/cc -DCHECK_FUNCTION_EXISTS=fann_get_errno CMakeFiles/cmTryCompileExec2973046031.dir/CheckFunctionExists.c.o -o cmTryCompileExec2973046031 -L/usr/local/lib -rdynamic -lfann -Wl,-rpath,/usr/local/lib
/usr/local/lib/libfann.so: undefined reference to 'sin'
/usr/local/lib/libfann.so: undefined reference to 'exp'
/usr/local/lib/libfann.so: undefined reference to 'cos'
/usr/local/lib/libfann.so: undefined reference to 'log'
/usr/local/lib/libfann.so: undefined reference to 'pow'
/usr/local/lib/libfann.so: undefined reference to 'sqrt'
/usr/local/lib/libfann.so: undefined reference to 'floor'`
in the subsequent if-statement the second variable is therefore undefined.
I suspect that this is because the test program is not linked with the standard math library. However in my main program the libm.so will be linked.
How do I fix the linking of the cmake test program?
I would be happy about any comments
Thank you
Arne
As per the documentation of CHECK_LIBRARY_EXISTS(), you can set CMAKE_REQUIRED_LIBRARIES to a list of libraries required to link the test before invoking CHECK_LIBRARY_EXISTS(). Like so:
set(CMAKE_REQUIRED_LIBRARIES m)
CHECK_LIBRARY_EXISTS(fann fann_get_errno "/usr/local/lib/" HAVE_FANN_LIB)
Related
Context: I am using stack to profile a build which uses FFI invocations to C++ code (via inline-C, a template haskell extension).
Problem: When running stack build, the project builds fine. But when running stack build --profile, however, I get the following g++ linker error:
$ stack build --profile
Sling-0.1.0.0: configure
Configuring Sling-0.1.0.0...
Sling-0.1.0.0: build
Preprocessing executable 'Sling-exe' for Sling-0.1.0.0...
[1 of 4] Compiling SimpleCompositor ( src/SimpleCompositor.hs, .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o )
[2 of 4] Compiling Struct ( src/Struct.hs, .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/Struct.p_o )
[3 of 4] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/Lib.p_o )
[4 of 4] Compiling Main ( main/Main.hs, .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/Main.p_o )
Linking .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe ...
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `c99J_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x77e): undefined reference to `inline_c_SimpleCompositor_5_c5df445469e64aa7738f9848cfc9b677b917df47'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90H_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x8e2): undefined reference to `inline_c_SimpleCompositor_4_4128a18e34f611cc08850d5bf36de9ac2707d1b2'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90I_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0xae2): undefined reference to `inline_c_SimpleCompositor_3_d0fe1ab601f5030423845b84b10160e1cbd9d79a'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90J_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0xdc2): undefined reference to `inline_c_SimpleCompositor_2_c9e46c413b75c4c01bb29f4321b9275f056c0911'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90K_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0xfc2): undefined reference to `inline_c_SimpleCompositor_1_fff40540e3a02ca3c27ae8f1c780462c1cbb82d0'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90L_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x11c2): undefined reference to `inline_c_SimpleCompositor_0_2e4a0d3a8c4b9baa2fc6947c1d65b62df44e96df'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `s91O_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x1438): undefined reference to `inline_c_SimpleCompositor_2_c9e46c413b75c4c01bb29f4321b9275f056c0911'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `s91W_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x18f0): undefined reference to `inline_c_SimpleCompositor_4_4128a18e34f611cc08850d5bf36de9ac2707d1b2'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `s925_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x1c62): undefined reference to `inline_c_SimpleCompositor_5_c5df445469e64aa7738f9848cfc9b677b917df47'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `s92u_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x2363): undefined reference to `inline_c_SimpleCompositor_0_2e4a0d3a8c4b9baa2fc6947c1d65b62df44e96df'
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x2534): undefined reference to `inline_c_SimpleCompositor_1_fff40540e3a02ca3c27ae8f1c780462c1cbb82d0'
collect2: error: ld returned 1 exit status
`g++' failed in phase `Linker'. (Exit code: 1)
-- While building package Sling-0.1.0.0 using:
/home/user/.stack/setup-exe-cache/x86_64-linux-ncurses6/setup-Simple-Cabal-1.24.0.0-ghc-8.0.1 --builddir=.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0 build exe:Sling-exe --ghc-options " -ddump-hi -ddump-to-file"
Process exited with code: ExitFailure 1
Weirdly enough: If I delete .stack-work from my project and then run the following sequence:
$ stack build
$ stack build --profile
The profiling works!
Why is this? Relying on this hack to get profiling working can't be good.
It looks that there are references being generated by Template Haskell that are not available when doing the profile.
As answered in this question, c-inline is differentiated from inline-c by the fact that it always generates the same identifiers basing the naming on the function contents.
That's why you could get those linker errors:
First, you build your project and it generates all the necessary references made with TH.
Second, when rebuilding the project with stack build --profile, incremental compilation is triggered and the identifiers are not created again, but the calls do, resulting in a linker error.
When you delete your .stack-work folder you force stack to rebuild the whole project and generate the identifiers correctly.
A solution could be relying on a Makefile that cleans the project cache by removing .stack-work.
I am working on this project and this is a related issue
i have replaced the TTS flite engine with Espeak TTs engine so I had to modify the CmakeLists.txt
find_package(Espeak REQUIRED)
include_directories(${Espeak_INCLUDE_DIRS})
set(LIBS ${LIBS} ${Espeak_LIBRARIES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR} /cmake/Modules/")
if (NOT Espeak_Found)
message(FATAL_ERROR "Package Espeak required, but not found!")
endif(NOT Espeak_Found)
because of that FindEspeak.cmake isn`t supported by default so I added the file to cmake default module path,compiled the project and get the following error
Linking CXX executable TextReading
CMakeFiles/TextReading.dir/EspeakBridge.cpp.o:(.bss+0x0): multiple definition of `samplerate'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:(.bss+0x0): first defined here
CMakeFiles/TextReading.dir/EspeakBridge.cpp.o:(.bss+0x10): multiple definition of `sounddata'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:(.bss+0x10): first defined here
CMakeFiles/TextReading.dir/EspeakBridge.cpp.o:(.bss+0x28): multiple definition of `counter'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:(.bss+0x28): first defined here
CMakeFiles/TextReading.dir/EspeakBridge.cpp.o:(.bss+0x30): multiple definition of `waves'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:(.bss+0x30): first defined here
CMakeFiles/TextReading.dir/EspeakBridge.cpp.o: In function `SynthCallback(short*, int, espeak_EVENT*)':
EspeakBridge.cpp:(.text+0x0): multiple definition of `SynthCallback(short*, int, espeak_EVENT*)'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:EspeakTTSWorker.cpp: (.text+0x0): first defined here
CMakeFiles/TextReading.dir/moc_EspeakTTSWorker.cxx.o:(.bss+0x0): multiple definition of `samplerate'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:(.bss+0x0): first defined here
CMakeFiles/TextReading.dir/moc_EspeakTTSWorker.cxx.o:(.bss+0x10): multiple definition of `sounddata'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:(.bss+0x10): first defined here
CMakeFiles/TextReading.dir/moc_EspeakTTSWorker.cxx.o:(.bss+0x28): multiple definition of `counter'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:(.bss+0x28): first defined here
CMakeFiles/TextReading.dir/moc_EspeakTTSWorker.cxx.o:(.bss+0x30): multiple definition of `waves'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:(.bss+0x30): first defined here
CMakeFiles/TextReading.dir/moc_EspeakTTSWorker.cxx.o: In function `SynthCallback(short*, int, espeak_EVENT*)':
moc_EspeakTTSWorker.cxx:(.text+0x0): multiple definition of `SynthCallback(short*, int, espeak_EVENT*)'
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o:EspeakTTSWorker.cpp:(.text+0x0): first defined here
CMakeFiles/TextReading.dir/EspeakTTSWorker.cpp.o: In function `EspeakTTSWorker::run()':
EspeakTTSWorker.cpp:(.text+0x118): undefined reference to `espeak_Initialize'
CMakeFiles/TextReading.dir/EspeakBridge.cpp.o: In function `EspeakBridge::init()':
EspeakBridge.cpp:(.text+0x93): undefined reference to `espeak_Initialize'
EspeakBridge.cpp:(.text+0x9d): undefined reference to `espeak_SetVoiceByName'
EspeakBridge.cpp:(.text+0xa7): undefined reference to `espeak_SetSynthCallback'
CMakeFiles/TextReading.dir/EspeakBridge.cpp.o: In function `EspeakBridge::close()':
EspeakBridge.cpp:(.text+0x173): undefined reference to `espeak_Terminate'
CMakeFiles/TextReading.dir/EspeakBridge.cpp.o: In function `EspeakTTSWorker::setText(std::string const&)':
EspeakBridge.cpp:(.text._ZN15EspeakTTSWorker7setTextERKSs[_ZN15EspeakTTSWorker7setTextERKSs]+0x121): undefined reference to `espeak_Synth'
EspeakBridge.cpp:(.text._ZN15EspeakTTSWorker7setTextERKSs[_ZN15EspeakTTSWorker7setTextERKSs]+0x126): undefined reference to `espeak_Synchronize'
collect2: error: ld returned 1 exit status
make[2]: *** [TextReading] Error 1
make[1]: *** [CMakeFiles/TextReading.dir/all] Error 2
make: *** [all] Error 2`
what should I do ?
I just edited Espeak parameters
############ Find ESPEAK TTS ############
find_path(LIBESPEAK_INCLUDE_DIRS
NAMES speak_lib.h
HINTS /usr/include/espeak)
find_library(LIBESPEAK_LIBRARIES
NAMES espeak
HINTS /usr/lib/ /usr/x86_64-linux-gnu/
PATH_SUFFIXES lib)
########################################
add_executable(TextReading
${DAD_SOURCES}
${DAD_HEADERS}
${MY_UI_HDRS}
${MY_MOC_SRCS}
${MY_CUDA_COMPILED_FILES}
${QEXTSERIALPORT_SOURCES} ${QEXTSERIALPORT_HEADERS}
)
I am trying to build an application under mingw with Chart Director. My code compiles fine, but I get undefined reference errors from the linker.
The object file needs to link with ../ChartDirector.cpp/lib/chartdir51.lib
I have renamed this file to ../ChartDirector.cpp/lib/libchartdir51.a If I do not do this the linker cannot find it.
The result is:
mingw32-g++.exe -LJ:\wxWidgets-3.0.1\lib\gcc481TDM_dll
-L..\ChartDirector_cpp\lib -o ..\bin\plot.exe obj\Debug\plot.o
-lwxbase30u -lwxmsw30u_core -lchartdir51 -mwindows
obj\Debug\plot.o: In function `ZN3BoxC2EP11BoxInternal':
C:\Users\James\Documents\code\vase\plot/../ChartDirector_cpp/include/chartdir.h:804:
undefined reference to `Box2DrawObj'
obj\Debug\plot.o: In function `ZN7TextBoxC1EP15TextBoxInternal':
C:\Users\James\Documents\code\vase\plot/../ChartDirector_cpp/include/chartdir.h:832:
undefined reference to `TextBox2Box'
... and so on
To make things simpler, I changed to trying to build the simplebar demo application supplied by Chart Director. The build command now looks like this:
mingw32-g++.exe
C:\Users\James\Documents\code\vase\test\simplebar.cpp
-IC:\Users\James\Documents\code\vase\ChartDirector_cpp\include
-o C:\Users\James\Documents\code\vase\bin\test.exe
-LC:\Users\James\Documents\code\vase\ChartDirector_cpp\lib\
-lchartdir51
I still have the same undefineds.
Following this recipe I created a def file looking like this
EXPORTS
Box2DrawObj
TextBox2Box
CBaseChart_destroy
CBaseChart_makeChart
CAxis_setLabels
BarLayer2Layer
CXYChart_create
XYChart2BaseChart
CXYChart_xAxis
CXYChart_setPlotArea
CXYChart_addBarLayer
and used the dlltool to create a new libchartdir51.a
dlltool -d C:\Users\James\Documents\code\vase\ChartDirector_cpp\lib\chartdir.def
-l C:\Users\James\Documents\code\vase\ChartDirector_cpp\lib\libchartdir51.a
Well that fixed the undefined I saw before, but now I am getting something even more mysterious:
C:\Users\James\Documents\code\vase\ChartDirector_cpp\lib/libchartdir51.a(diaybs00001.o):(.idata$7+0x
0):
undefined reference to `_head_C__Users_James_Documents_code_vase_ChartDirector_cpp_lib_libchartd
ir51_a'
C:\Users\James\Documents\code\vase\ChartDirector_cpp\lib/libchartdir51.a(diaybs00009.o):(.idata$7+0x
0): undefined reference to `_head_C__Users_James_Documents_code_vase_ChartDirector_cpp_lib_libchartd
ir51_a'
... and so on
I tried downloading the linux version of chartdir. The result was:
mingw32-g++.exe
C:\Users\James\Downloads\ChartDirector\cppdemo\simplebar\simplebar.cpp
-IC:\Users\James\Downloads\ChartDirector\include
-o C:\Users\James\code\vase\bin\test.exe
C:\Users\James\Downloads\ChartDirector\lib\libchartdir.so.5.1.0
C:\Users\James\Downloads\ChartDirector\lib\libchartdir.so.5.1.0:
could not read symbols: File in wrong format
collect2.exe: error: ld returned 1 exit status
The problem was that I was trying to build with a 64 bit version of ChartDirector on a 32 bit mingw. Downloading the 32 bit library fixes the problem:
mingw32-g++
C:\Users\James\Downloads\chartdir_cpp_win32\ChartDirector\cppdemo\simplebar\simplebar.cpp
-IC:\Users\Jam es\Downloads\chartdir_cpp_win32\ChartDirector\include
-o C:\Users\James\code\vase\bin\test2.exe
-C:\Users\James\Downloads\chartdir_cpp_win32\ChartDirector\lib\chartdir51.dll
Normally undefined reference errors comes when the libs are not matching. Always get the latest libs from ChartDirector download page.
I installed the CodeSourcery g++ toolchain and tried to compile a simple hello world program:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
And got a lot of errors from the linker
$ arm-none-eabi-g++ helloworld.cpp -o helloworld.exe
bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000008018
lib/libc.a(lib_a-abort.o): In function `abort':
abort.c:(.text.abort+0x10): undefined reference to `_exit'
lib/libc.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text._fstat_r+0x1c): undefined reference to `_fstat'
lib/libc.a(lib_a-openr.o): In function `_open_r':
openr.c:(.text._open_r+0x20): undefined reference to `_open'
lib/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0x18): undefined reference to `_sbrk'
lib/libc.a(lib_a-signalr.o): In function `_kill_r':
signalr.c:(.text._kill_r+0x1c): undefined reference to `_kill'
lib/libc.a(lib_a-signalr.o): In function `_getpid_r':
signalr.c:(.text._getpid_r+0x4): undefined reference to `_getpid'
lib/libc.a(lib_a-writer.o): In function `_write_r':
writer.c:(.text._write_r+0x20): undefined reference to `_write'
lib/libc.a(lib_a-closer.o): In function `_close_r':
closer.c:(.text._close_r+0x18): undefined reference to `_close'
lib/libc.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text._isatty_r+0x18): undefined reference to `_isatty'
lib/libc.a(lib_a-lseekr.o): In function `_lseek_r':
lseekr.c:(.text._lseek_r+0x20): undefined reference to `_lseek'
lib/libc.a(lib_a-readr.o): In function `_read_r':
readr.c:(.text._read_r+0x20): undefined reference to `_read'
collect2: ld returned 1 exit status
What library am I missing here?
The GCC toolchain is only half of what you need to create a working executable*. The other half is the runtime library. The runtime includes crt0.o, which contains the entry point (the code that calls main()), and generally a libc that contains the standard C functions (strcmp(), memcpy(), etc) as well as the system calls (open(), read(), and others). You need to find a source for these. If you're targeting an embedded Linux or BSD machine, you'll have to find out what libc your target is using. It's probably either GNU libc, BSD libc, newlib, or uclibc. You can get these and build them yourself, or they may be available already with your OS.
*unless you're building a freestanding binary, but this doesn't look to be what you're doing.
Basing on the cross compiler it appears that end target is bare metal. The default gcc libraries (OS dependent) will not work here. You can use the NewlibC or NewlibC-Nano. This generally will be shipped along with the cross compiler tool chain.
Look for lib folder which has precompiled NewlibC.
Once that is done,
This statement might help you to an extent.
arm-none-eabi-gcc --specs=rdimon.specs \
-Wl,--start-group -lgcc -lc -lc -lm -lrdimon -Wl,--end-group $(OTHER_OPTIONS)
Don't forget to provide the path of looking the libraries -L(path).
I had this same problem.
It turns out there are a handful of options you can send to the linker to make it start working, but the one that had the least splash damage in my code base was
-nostartfiles
I'm trying to compile a project that uses both libjpeg and libpng. I know that libpng needs zlib, so I compiled all the three independently and put them (libjpeg.a, libpng.a and libz.a) on a folder called linrel32. What I execute then is:
g++ -Llinrel32/ program.cpp otherfile.cpp -o linrel32/executable -Izlib/ -Ilpng140/ -Ijpeg/ -lpthread -lX11 -O2 -DLINUX -s -lz -lpng -ljpeg
So I include the three libraries. Still, the linker complains:
linrel32//libpng.a(png.o): In function `png_calculate_crc':
png.c:(.text+0x97d): undefined reference to `crc32'
linrel32//libpng.a(png.o): In function `png_reset_crc':
png.c:(.text+0x9be): undefined reference to `crc32'
linrel32//libpng.a(png.o): In function `png_reset_zstream':
png.c:(.text+0x537): undefined reference to `inflateReset'
linrel32//libpng.a(pngread.o): In function `png_read_destroy':
pngread.c:(.text+0x6f4): undefined reference to `inflateEnd'
linrel32//libpng.a(pngread.o): In function `png_read_row':
pngread.c:(.text+0x1267): undefined reference to `inflate'
linrel32//libpng.a(pngread.o): In function `png_create_read_struct_2':
(... you get the idea :D)
collect2: ld returned 1 exit status
I know the missing functions are from zlib, and I'm adding zlib there. Opened libz.a and it seems to have a good structure. Recompiled it, everything looks fine. But it is not...
I don't know, is likely that the problem is trivial, and what I need is to sleep for a while. But still, if you could help me to figure out this thing ...
You need to rearrange the order of the libraries:
-lpng -ljpeg -lz
What is happening is that the linker has special rules on how it treats static libraries. What it does is that it only includes a .o from inside the .a if the .o is needed to satisfy a reference.
Furthermore, it handles static archives in the order in which they appear on the link line.
So, your code does not directly call any functions in zlib. So when the linker handles -lz first, there are not yet any calls to it so it doesn't pull in any of zlib.
Next, when the linker handles libpng, it sees that there are calls to it from your code. So it pulls the code from libpng and since it makes calls to zlib, now there are references to the zlib functions.
Now you come to the end of your libraries and there are unsatisfied calls which causes your error.
So, if libhigh.a makes use of liblow.a, you must have -lhigh before -llow in your link order.
you probably need to surround the zlib and png headers with extern "C", e.g.:
extern "C" {
#include <zlib.h>
}