Search code examples
c++cmakeintel-oneapi

Linker issue with oneAPI if I use the -ipo flag


I'm using oneAPI (intel's compiler) and ran into issues using the -ipo flag when compiling. The setup:

  • my_lib project: static lib
  • my_test: executable that links to my_lib

Files: my_lib:

  • main.cpp
    int foo() {
        return 0;    
    }
    
  • CMakeLists
    project(my_lib CXX)
    add_library(my_lib STATIC main.cpp)
    

my_test:

  • main.cpp
    #include <iostream>
    
    using namespace std;
    int main() {
        cout << "Hello World" << endl;
    }
    
  • CMakeLists
    project(my_test)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ipo")
    
    add_subdirectory(my_lib)
    
    add_executable(my_test main.cpp)
    target_link_libraries(my_test my_lib)
    

Folder structure looks like this:

  • main.cpp
  • CMakeLists.txt // this one's for my_test
  • my_lib
    • main.cpp
    • CMakeLists.txt

I'd run cmake on my_test's CMakeLists.txt, then run make. Thee error I get is:

...
...
[100%] Linking CXX executable my_test
/usr/bin/ld: my_lib/my_lib.a: error adding symbols: archive has no index; run ranlib to add one
icpx: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/my_test.dir/build.make:98: my_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/my_test.dir/all] Error 2

Any ideas why I can't link this simple code?


Solution

  • I read somewhere that the default GNU linker wouldn't be able to make sense of binaries built using the Intel compiler with the -ipo flag on. I switched to using the LLVM linker and it worked.

    To turn that on, I added this to the CMakeLists file

    add_link_options("-fuse-ld=lld")