'make' 的输出是共享对象而不是可执行文件

Output of 'make' is a shared object and not an executable

编辑:这似乎是一个 g++ 问题,因为使用 clang++ 编译会输出一个可执行文件。

我编写了一个具有主要功能的 C++ 应用程序,创建一个应用程序 window,加载 3D fbx 文件并使用 opengl 绘制该文件。要创建用于编译的 Makefile,我使用 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 2.8)
project(solight)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
INCLUDE_DIRECTORIES(lib/include)

###########################################
#SET THIS TO X32 IN CASE OF A 32 BIT SYSTEM
###########################################
set (ARCH x64)

set (SRC_LIST
    src/assetmanager.cpp src/assetmanager.h
    src/mesh.cpp src/mesh.h
    src/model.cpp src/model.h
    src/modelloader.h
    src/main.cpp
    src/math.h
    src/fbxmodelloader.cpp src/fbxmodelloader.h
    src/rendermodule.h
    src/openglrendermodule.cpp src/openglrendermodule.h
    src/textureloader.h
    src/engine.cpp src/enginemodules.cpp src/engine.h
)


##########################
#EXTERNAL LIBRARY HANDLING
##########################

set (LINUX_DEPS
    libfbxsdk.a
    pthread
    libSDL2.a
    GL
    libGLEW.a
    dl
)

set (WIN32_DEPS
)

set (APPLE_DEPS
)

if (UNIX AND NOT APPLE)
    set (DEPS ${LINUX_DEPS})
    set (OS Linux)
endif()

if (APPLE)
    set (DEPS ${APPLE_DEPS})
    set (OS Apple)
endif()

if (WIN32)
    set (DEPS ${WIN32_DEPS})
    set (OS WIN32)
endif()


LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/lib/${OS}/${ARCH})

####################
#EXECUTBALE CREATION
####################

add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${DEPS})

所以在 运行 生成 cmake 创建的 makefile 之后,输出不是预期的可执行文件,而是一个共享对象文件。如果我 运行 上面的文件命令是输出:

solight: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=f20c07c8743a70bca20d4a0d9f50fcb108b8140e, not stripped

执行时

 /lib64/ld-linux-x86-64.so.2 ./solight --verify

程序按预期执行。

但是当我通过终端执行文件时它运行很好,创建window并渲染模型。

关于为什么这是共享对象文件的任何解释? 提前致谢。

编辑: 当运行宁

make VERBOSE=1

输出为:

    /usr/bin/cmake -H/home/wouter/Documents/Solight -B/home/wouter/Documents/Solight/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/wouter/Documents/Solight/build/CMakeFiles /home/wouter/Documents/Solight/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/wouter/Documents/Solight/build'
make -f CMakeFiles/solight.dir/build.make CMakeFiles/solight.dir/depend
make[2]: Entering directory '/home/wouter/Documents/Solight/build'
cd /home/wouter/Documents/Solight/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/wouter/Documents/Solight /home/wouter/Documents/Solight /home/wouter/Documents/Solight/build /home/wouter/Documents/Solight/build /home/wouter/Documents/Solight/build/CMakeFiles/solight.dir/DependInfo.cmake --color=
Dependee "/home/wouter/Documents/Solight/build/CMakeFiles/solight.dir/DependInfo.cmake" is newer than depender "/home/wouter/Documents/Solight/build/CMakeFiles/solight.dir/depend.internal".
Dependee "/home/wouter/Documents/Solight/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/wouter/Documents/Solight/build/CMakeFiles/solight.dir/depend.internal".
Scanning dependencies of target solight
make[2]: Leaving directory '/home/wouter/Documents/Solight/build'
make -f CMakeFiles/solight.dir/build.make CMakeFiles/solight.dir/build
make[2]: Entering directory '/home/wouter/Documents/Solight/build'
[ 11%] Building CXX object CMakeFiles/solight.dir/src/assetmanager.cpp.o
/usr/bin/c++    -I/home/wouter/Documents/Solight/lib/include  -std=c++14   -o CMakeFiles/solight.dir/src/assetmanager.cpp.o -c /home/wouter/Documents/Solight/src/assetmanager.cpp
[ 22%] Building CXX object CMakeFiles/solight.dir/src/mesh.cpp.o
/usr/bin/c++    -I/home/wouter/Documents/Solight/lib/include  -std=c++14   -o CMakeFiles/solight.dir/src/mesh.cpp.o -c /home/wouter/Documents/Solight/src/mesh.cpp
[ 33%] Building CXX object CMakeFiles/solight.dir/src/model.cpp.o
/usr/bin/c++    -I/home/wouter/Documents/Solight/lib/include  -std=c++14   -o CMakeFiles/solight.dir/src/model.cpp.o -c /home/wouter/Documents/Solight/src/model.cpp
[ 44%] Building CXX object CMakeFiles/solight.dir/src/main.cpp.o
/usr/bin/c++    -I/home/wouter/Documents/Solight/lib/include  -std=c++14   -o CMakeFiles/solight.dir/src/main.cpp.o -c /home/wouter/Documents/Solight/src/main.cpp
[ 55%] Building CXX object CMakeFiles/solight.dir/src/fbxmodelloader.cpp.o
/usr/bin/c++    -I/home/wouter/Documents/Solight/lib/include  -std=c++14   -o CMakeFiles/solight.dir/src/fbxmodelloader.cpp.o -c /home/wouter/Documents/Solight/src/fbxmodelloader.cpp
[ 66%] Building CXX object CMakeFiles/solight.dir/src/openglrendermodule.cpp.o
/usr/bin/c++    -I/home/wouter/Documents/Solight/lib/include  -std=c++14   -o CMakeFiles/solight.dir/src/openglrendermodule.cpp.o -c /home/wouter/Documents/Solight/src/openglrendermodule.cpp
[ 77%] Building CXX object CMakeFiles/solight.dir/src/engine.cpp.o
/usr/bin/c++    -I/home/wouter/Documents/Solight/lib/include  -std=c++14   -o CMakeFiles/solight.dir/src/engine.cpp.o -c /home/wouter/Documents/Solight/src/engine.cpp
[ 88%] Building CXX object CMakeFiles/solight.dir/src/enginemodules.cpp.o
/usr/bin/c++    -I/home/wouter/Documents/Solight/lib/include  -std=c++14   -o CMakeFiles/solight.dir/src/enginemodules.cpp.o -c /home/wouter/Documents/Solight/src/enginemodules.cpp
[100%] Linking CXX executable solight
/usr/bin/cmake -E cmake_link_script CMakeFiles/solight.dir/link.txt --verbose=1
/usr/bin/c++    -std=c++14   CMakeFiles/solight.dir/src/assetmanager.cpp.o CMakeFiles/solight.dir/src/mesh.cpp.o CMakeFiles/solight.dir/src/model.cpp.o CMakeFiles/solight.dir/src/main.cpp.o CMakeFiles/solight.dir/src/fbxmodelloader.cpp.o CMakeFiles/solight.dir/src/openglrendermodule.cpp.o CMakeFiles/solight.dir/src/engine.cpp.o CMakeFiles/solight.dir/src/enginemodules.cpp.o  -o solight  -L/home/wouter/Documents/Solight/lib/Linux/x64 -rdynamic -Wl,-Bstatic -lfbxsdk -Wl,-Bdynamic -lpthread -Wl,-Bstatic -lSDL2 -Wl,-Bdynamic -lGL -Wl,-Bstatic -lGLEW -Wl,-Bdynamic -ldl -Wl,-rpath,/home/wouter/Documents/Solight/lib/Linux/x64 
/home/wouter/Documents/Solight/lib/Linux/x64/libfbxsdk.a(fbxutils.o): In function `fbxsdk_2015_1::FbxPathUtils::GenerateFileName(char const*, char const*)':
(.text+0x4c8): warning: the use of `tempnam' is dangerous, better use `mkstemp'
make[2]: Leaving directory '/home/wouter/Documents/Solight/build'
[100%] Built target solight
make[1]: Leaving directory '/home/wouter/Documents/Solight/build'
/usr/bin/cmake -E cmake_progress_start /home/wouter/Documents/Solight/build/CMakeFiles 0

当使用特殊 CFLAGS 编译应用程序时可能会发生这种情况,例如-pie -fPIE:

$ echo 'int main() { return 0; }' | gcc -x c - -fPIE -pie
$ file a.out
a.out: ELF 64-bit LSB  shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, 

也许您可以 运行 与 VERBOSE=1 合作,看看是不是这样?通常 file 可能会使用启发式方法来识别文件类型,因此您不应过度依赖它。

关于 ld.so 的错误,您使用了错误的 32 位动态链接器 运行 64 位应用程序。请改用 /lib64/ld-linux-x86-64.so.2(正如 file 告诉您的那样)。

编辑:另一种选择是您的 GCC 是使用 --enable-default-pie which seems to be the case for modern Ubuntu 构建的。您可以通过使用 CFLAGS=-no-pie(或 -nopie,取决于 GCC 版本)来禁用此功能,但我宁愿不这样做 - PIE 的可执行文件允许 ASLR 做得更好,从而使您的系统更安全。

我发现根本原因是 CMAKE_EXE_LINKER_FLAGS 中的 -shared 标志。 当我删除 -shared 时,一切正常。