Resolving a Haskell `stack build --profile` g++ Linker Error - g++

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.

Related

Compiling object oriented fortran with gfortran [duplicate]

I'm having trouble trying to compile a simple fortran program which uses a module in the same directory.
I have 2 files: test1.f90 which contains the program and modtest.f90 which contains the module.
This is test1.f90:
program test
use modtest
implicit none
print*,a
end program test
This is modtest.f90:
module modtest
implicit none
save
integer :: a = 1
end module modtest
Both files are in the same directory. I compile modtest.f90 and test.f90 like this:
gfortran -c modtest.f90
gfortran -o test1 test1.f90
But then I get this error:
/tmp/cckqu8c3.o: In function `MAIN__':
test1.f90:(.text+0x50): undefined reference to `__modtest_MOD_a'
collect2: ld returned 1 exit status
Is there something I'm missing?
Thanks for the help
What you're doing is not telling the linker where reference module modtest is so that the your code can use its contents.
This should work:
gfortran -o test1 test1.f90 modtest.o
Some context:
The -o option tells the compiler to put the output of the full build (compile + link) into a program called test1. Then we supply a file that we are to compile (test1.f90). Finally we are telling the compiler to consider a file that contains the compiled output of another build (modtest.o) and to link this to the compiled output of test1.f90, and use the contents of modtest.o when trying to sort out references within the test1.f90 that reference the module modtest (in the statement use modtest in the source code).
So the statement says:
Please compile and subsequently link test1.f90 to modtest.o, and produce a file called test1 as the final output.

I have opened .xcworkspace and when run application am getting clang: error: linker command failed with exit code 1 (use -v to see invocation)

duplicate symbol _GTMNSDictionaryURLArgumentsExportToSuppressLibToolWarning in:
/Users/Varshana/Library/Developer/Xcode/DerivedData/Lodore-axtsfmjdyxbqyccujoquacrynsxv/Build/Products/Debug-iphonesimulator/GoogleToolboxForMac/libGoogleToolboxForMac.a(GTMNSDictionary+URLArguments.o)
/Users/Varshana/Documents/Lodore final stage/GoogleSignInDependencies.framework/GoogleSignInDependencies(GTMNSDictionary+URLArguments_aec9c2446af4c2ac5a7380b418b337ea.o)
duplicate symbol _kGTMOAuth2WebViewKey in:
/Users/Varshana/Library/Developer/Xcode/DerivedData/Lodore-axtsfmjdyxbqyccujoquacrynsxv/Build/Products/Debug-iphonesimulator/GTMOAuth2/libGTMOAuth2.a(GTMOAuth2Authentication.o)
/Users/Varshana/Documents/Lodore final stage/GoogleSignInDependencies.framework/GoogleSignInDependencies(GTMOAuth2Authentication_ad53759ab562393658e6c193e56628dd.o)
duplicate symbol _kGTMOAuth2ErrorRequestKey in:
/Users/Varshana/Library/Developer/Xcode/DerivedData/Lodore-axtsfmjdyxbqyccujoquacrynsxv/Build/Products/Debug-iphonesimulator/GTMOAuth2/libGTMOAuth2.a(GTMOAuth2Authentication.o)
/Users/Varshana/Documents/Lodore final stage/GoogleSignInDependencies.framework/GoogleSignInDependencies(GTMOAuth2Authentication_ad53759ab562393658e6c193e56628dd.o)
don't worry, just xcode bug. try clean and build and it will work, or better, go to xcode->preferences->location->Derived Data. delete all files inside derived data and then restart xcode. clean and build the project and then it will work perfectly
ps: updated for cleaning
Cmd + Shift + Alt + K
To check the exact error for this issue check following:
Select issue navigator > Click on error will show logs for error > In that select All Messages tab.
75 duplicate symbols for architecture x86_64
Means that you have loaded same functions twice. As the issue disappear after removing -ObjC from Other Linker Flags, this means that this option result that functions loads twice:
from Technical Q&A
This flag causes the linker to load every object file in the library
that defines an Objective-C class or category. While this option will
typically result in a larger executable (due to additional object code
loaded into the application), it will allow the successful creation of
effective Objective-C static libraries that contain categories on
existing classes.
https://developer.apple.com/library/content/qa/qa1490/_index.html

undefined reference to espeak using cmake

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}
)

CHECK_LIBRARY_EXISTS library with dependencies

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)

How to link with chart director library with mingw

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.