-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failed to link Clang built static library #151
Comments
Additional note: we have many other third parties, that are also build with same settings, and if I remove only this project and the relevant code, everything links fine |
I'm facing the same problem here. If your check the |
Yeah, using lld's instead of the native ld works (it knows how to link LLVM IR bitcode archives as static libs). If you don't want to use lld, then probably you either should turn Line 7 in 095ff84
|
Here is some investigation...
So this issue is probably related to this https://clang.llvm.org/docs/ThinLTO.html
|
I'd suggest that this may be a nice addition to the FAQ & Troubleshooting. |
…e with compilers. related awslabs#151
…e with compilers. related awslabs#151
…e with compilers. related #151 (#163) Co-authored-by: Bryan Moffatt <[email protected]>
|
@marcomagdy , this is how to reproduce the error: FROM ubuntu:20.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y git cmake clang libcurl4-openssl-dev \
&& rm -rf /var/lib/apt/lists/*
RUN git clone --branch=master --depth=1 https://github.com/awslabs/aws-lambda-cpp.git /aws-lambda-cpp \
&& mkdir /aws-lambda-cpp/build \
&& cd /aws-lambda-cpp/build \
&& cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/lambda-install \
&& make -j4 \
&& make install \
&& make clean
WORKDIR /my-lambda
# CMakeLists.txt AS-IS from README.md
RUN printf '\
cmake_minimum_required(VERSION 3.9)\n\
set(CMAKE_CXX_STANDARD 11)\n\
project(demo LANGUAGES CXX)\n\
find_package(aws-lambda-runtime)\n\
add_executable(${PROJECT_NAME} "main.cpp")\n\
target_link_libraries(${PROJECT_NAME} PRIVATE AWS::aws-lambda-runtime)\n\
target_compile_features(${PROJECT_NAME} PRIVATE "cxx_std_11")\n\
target_compile_options(${PROJECT_NAME} PRIVATE "-Wall" "-Wextra")\n\
aws_lambda_package_target(${PROJECT_NAME})\n\
' > /my-lambda/CMakeLists.txt
# main.cpp AS-IS from README.md
RUN printf \
'#include <aws/lambda-runtime/runtime.h>\n\
\n\
using namespace aws::lambda_runtime;\n\
\n\
static invocation_response my_handler(invocation_request const& req)\n\
{\n\
if (req.payload.length() > 42) {\n\
return invocation_response::failure("error message here"/*error_message*/,\n\
"error type here" /*error_type*/);\n\
}\n\
\n\
return invocation_response::success("json payload here" /*payload*/,\n\
"application/json" /*MIME type*/);\n\
}\n\
\n\
int main()\n\
{\n\
run_handler(my_handler);\n\
return 0;\n\
}\n\
' > /my-lambda/main.cpp
RUN mkdir build \
&& cd build \
&& cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/lambda-install \
&& make -j4 VERBOSE=1 \
&& make clean and this:
gives me exactly this same error:
Notice that It's just a clean ubuntu with clang installed which I think it might be a pretty common setup. Clang's with LTO enabled outputs LLVM IR bitcode inside
Please let me know if I can help somehow in a deeper analysis. Thank you for your time on this. |
Indeed the root cause of this is a conflicted toolchain. Observations from playing with that Dockerfile
So, if I were an Ubuntu user who also preferred clang to gcc, my Dockerfile would also include:
|
Yes, if using LTO I agree the best option is indeed to use lld, either by setting the ld symbolic link like you did or just passing it to the compiler flags for this project build specifically with Anyway, thanks for the analysis. This is what motivated me in trying to add a faq & troubleshooting section for helping solving this. |
Note that if you build with Clang and GNU's ld with LTO turned on, you are able to produce a shared-library (.so file) for the runtime just fine. |
Moreover, if you enable LTO in the app, GNU's ld links it all just fine. I just tried it. So, it has nothing to do with GNU's ld not understanding LLVM IR as mentioned in the now merged README PR. cc @bmoffatt For posterity, here's how to enable LTO in CMake: set_property(TARGET ${PROJECT_NAME} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) |
I think the right thing to do at this point is:
I think this problem bites a lot of people because CMake currently (unless explicitly told otherwise) enables LTO and builds the runtime as a static library by default. And since we don't have evidence that LTO makes a substantial difference, it's best to change that default rather than building the library as a shared object. |
Yes, this works because it will use the LLVM GoldPlugin with Gnu ld.
You would see this:
Note the This is what I meant in the item 2:
I also quoted this from its own docs:
So it works but LLVM doesn't actually recommend this approach, unfortunately. I totally agree with your final reasoning here:
Thanks once again for this whole analysis session. |
Thank you for your patience and for following up. |
LTO has caused problems to a few users (see issues linked at the bottom). The problem is the runtime is built as a static library by default unless otherwise specified via CMake flags, and also LTO is enabled by default. Those two things combined means the user must turn on LTO when they build their application if they're using GCC. Since we don't have evidence that LTO is making a substantial difference in the runtime, it seems prudent to leave the option but turn if off by default. Issues where this has been reported: awslabs#151 awslabs#128
Hi,
I have a CMake project, where I build aws-lambda-cpp as external project with Clang-14:
`
set(TESSER_THIRDPARTY_CMAKE_CACHE_ARGS
-DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
-DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD}
-DCMAKE_INSTALL_PREFIX:PATH=${PROJECT_SOURCE_DIR}/bin
)
ExternalProject_Add(
aws_lambda_cpp
GIT_REPOSITORY https://github.com/awslabs/aws-lambda-cpp.git
GIT_TAG v0.2.7
SOURCE_DIR "${PROJECT_SOURCE_DIR}/aws-lambda-cpp"
BUILD_ALWAYS 1
UPDATE_COMMAND ""
CMAKE_CACHE_ARGS
${TESSER_THIRDPARTY_CMAKE_CACHE_ARGS}
)
`
when I link it against to my final executable, I get the following linker error:
/usr/bin/ld: /home/<...>bin/lib/libaws-lambda-runtime.a: error adding symbols: file format not recognized clang: error: linker command failed with exit code 1 (use -v to see invocation)
The CMake target:
target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES} Boost::json Boost::log Boost::log_setup mongo::mongocxx_static mongo::bsoncxx_static AWS::aws-lambda-runtime pthread odbc)
Can anybody help me, what did I wrong?
Please let me know, if additional information needed.
Thank you
The text was updated successfully, but these errors were encountered: