CMake file being added in linking step - cmake

I have a project with several sub directories I'm compiling with CMake. Sometimes, when I do mkdir build && cd build && cmake .. && cmake --build . I get g++: error: mysub: linker input file not found. For some reason, on the link step, there is a mysub added to the compilation command. It happens after the includes. Sometimes, cleaning the build directory and rerunning cmake .. will fix it, sometimes it won't.
The options I'm telling CMake to use are:
function(addcxxwarn opt)
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:${opt}>")
endfunction()
addcxxwarn(${name} -Wall)
addcxxwarn(${name} -Wextra)
addcxxwarn(${name} -Wshadow)
addcxxwarn(${name} -Wpointer-arith)
addcxxwarn(${name} -Wcast-qual)
addcxxwarn(${name} -Wswitch-default)
addcxxwarn(${name} -Wcast-align)
addcxxwarn(${name} -Wundef)
addcxxwarn(${name} -Wwrite-strings)
addcxxwarn(${name} -Wconversion)
addcxxwarn(${name} -Wunreachable-code)
addcxxwarn(${name} -pedantic)
addcxxwarn(${name} -funroll-loops)
addcxxwarn(${name} -fno-common)
addcxxwarn(${name} -fno-exceptions)
addcxxwarn(${name} -ffloat-store)
addcxxwarn(${name} -fstrict-aliasing)
link_libraries(gsl gslcblas)
if (UNIX AND NOT APPLE)
include_directories(/opt/local/include)
link_directories(/opt/local/lib /opt/local/lib/libgcc)
link_libraries(gfortran blitz)
elseif (UNIX)
include_directories(/home/fitzpatrickr/include)
link_directories(/home/fitzpatrickr/lib)
link_libraries(gfortran.5)
endif()
add_compile_options("$<$<CONFIG:Debug>:-g;-Og;-Werror>")
add_compile_options("$<$<CONFIG:Release>:-O3>")
add_compile_options("$<$<CONFIG:RelWithDebInfo>:-g;-O3>")
and
configure_file(macro_defs.h.in macro_defs.h #ONLY)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
I don't see anywhere else where things are added to link or include options.

Related

How CMake automatically detects header dependencies

I wonder how CMake automatically detects that main.cpp depends on header.h
// header.h
int f() {
return 0;
}
// main.cpp
#include "header.h"
int main() {
return f();
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(Cppref)
add_executable(main main.cpp)
When I run cmake . -B build it creates the following make target in ./build/CMakeFiles/main.dir/build.make
CMakeFiles/main.dir/main.cpp.o: CMakeFiles/main.dir/compiler_depend.ts
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/nikolay/Cpp/Train/Cppref/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building CXX object CMakeFiles/main.dir/main.cpp.o"
/usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/nikolay/Cpp/Train/Cppref/main.cpp
Pay attention to the -MD compiler option, as
it is used to dump dependencies visible to the preprocessor.
So after the first build it will create ./build/CMakeFiles/main.dir/main.cpp.o.d with the following content
CMakeFiles/main.dir/main.cpp.o: /home/nikolay/Cpp/Train/Cppref/main.cpp \
/home/nikolay/Cpp/Train/Cppref/header.h
So whenever you change header.h, the target main.o will be rebuilt.

multiple definions of functions gcc/g++ __multi3 __negti2 etc

hi I have a sgx project that compile and run with a makefile. The trsted part is compiled with c++ 11 while the untrusted part is compiled with c++ 17.
I am trying now to compile with cmake. I put all the flags I used in the Makefile.. (of course something is not right). It compile fine, but return a lot of linking errors:
[ 64%] Linking CXX executable cscore
/usr/local/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_muldi3.o): in function `__multi3':
(.text+0x0): multiple definition of `__multi3'; /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_muldi3.o):(.text+0x0): first defined here
/usr/local/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_negdi2.o): in function `__negti2':
(.text+0x0): multiple definition of `__negti2'; /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_negdi2.o):(.text+0x0): first defined here
/usr/local/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_lshrdi3.o): in function `__lshrti3':
(.text+0x0): multiple definition of `__lshrti3'; /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_lshrdi3.o):(.text+0x0): first defined here
/usr/local/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_ashldi3.o): in function `__ashlti3':
(.text+0x0): multiple definition of `__ashlti3'; /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_ashldi3.o):(.text+0x0): first defined here
/usr/local/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_ashrdi3.o): in function `__ashrti3':
(.text+0x0): multiple definition of `__ashrti3'; /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a(_ashrdi3.o):(.text+0x0): first defined here
here is the cmake code
# FindPackage cmake file for Intel SGX SDK
cmake_minimum_required(VERSION 2.8.11) # target_include_directories
include(CMakeParseArguments)
set(SGX_FOUND "NO")
if(EXISTS SGX_DIR)
set(SGX_PATH ${SGX_DIR})
elseif(EXISTS SGX_ROOT)
set(SGX_PATH ${SGX_ROOT})
elseif(EXISTS $ENV{SGX_SDK})
set(SGX_PATH $ENV{SGX_SDK})
elseif(EXISTS $ENV{SGX_DIR})
set(SGX_PATH $ENV{SGX_DIR})
elseif(EXISTS $ENV{SGX_ROOT})
set(SGX_PATH $ENV{SGX_ROOT})
else()
set(SGX_PATH "/opt/intel/sgxsdk")
endif()
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(SGX_COMMON_CFLAGS -m32)
set(SGX_LIBRARY_PATH ${SGX_PATH}/lib32)
set(SGX_ENCLAVE_SIGNER ${SGX_PATH}/bin/x86/sgx_sign)
set(SGX_EDGER8R ${SGX_PATH}/bin/x86/sgx_edger8r)
else()
set(SGX_COMMON_CFLAGS -m64)
set(SGX_LIBRARY_PATH ${SGX_PATH}/lib64)
set(SGX_ENCLAVE_SIGNER ${SGX_PATH}/bin/x64/sgx_sign)
set(SGX_EDGER8R ${SGX_PATH}/bin/x64/sgx_edger8r)
endif()
find_path(SGX_INCLUDE_DIR sgx.h "${SGX_PATH}/include" NO_DEFAULT_PATH)
find_path(SGX_LIBRARY_DIR libsgx_urts.so "${SGX_LIBRARY_PATH}" NO_DEFAULT_PATH)
if(SGX_INCLUDE_DIR AND SGX_LIBRARY_DIR)
set(SGX_FOUND "YES")
set(SGX_INCLUDE_DIR "${SGX_PATH}/include" CACHE PATH "Intel SGX include directory" FORCE)
set(SGX_TLIBC_INCLUDE_DIR "${SGX_INCLUDE_DIR}/tlibc" CACHE PATH "Intel SGX tlibc include directory" FORCE)
set(SGX_LIBCXX_INCLUDE_DIR "${SGX_INCLUDE_DIR}/libcxx" CACHE PATH "Intel SGX libcxx include directory" FORCE)
set(SGX_INCLUDE_DIRS ${SGX_INCLUDE_DIR} ${SGX_TLIBC_INCLUDE_DIR} ${SGX_LIBCXX_INCLUDE_DIR})
mark_as_advanced(SGX_INCLUDE_DIR SGX_TLIBC_INCLUDE_DIR SGX_LIBCXX_INCLUDE_DIR SGX_LIBRARY_DIR)
message(STATUS "Found Intel SGX SDK.")
endif()
if(SGX_FOUND)
set(SGX_HW OFF CACHE BOOL "Run SGX on hardware, OFF for simulation.")
set(SGX_MODE Debug CACHE STRING "SGX build mode: Debug; PreRelease; Release.")
if(SGX_HW)
set(SGX_URTS_LIB sgx_urts)
set(SGX_USVC_LIB sgx_uae_service)
set(SGX_TRTS_LIB sgx_trts)
set(SGX_TSVC_LIB sgx_tservice)
else()
set(SGX_URTS_LIB sgx_urts_sim)
set(SGX_USVC_LIB sgx_uae_service_sim)
set(SGX_TRTS_LIB sgx_trts_sim)
set(SGX_TSVC_LIB sgx_tservice_sim)
endif()
if(SGX_MODE STREQUAL "Debug")
set(SGX_COMMON_CFLAGS "${SGX_COMMON_CFLAGS} -O0 -g -DSGX_DEBUG -DDEBUG -UNDEBUG -UEDEBUG")
elseif(SGX_MODE STREQUAL "PreRelease")
set(SGX_COMMON_CFLAGS "${SGX_COMMON_CFLAGS} -O2 -UDEBUG -DNDEBUG -DEDEBUG")
elseif(SGX_MODE STREQUAL "Release")
set(SGX_COMMON_CFLAGS "${SGX_COMMON_CFLAGS} -O2 -UDEBUG -DNDEBUG -UEDEBUG")
else()
message(FATAL_ERROR "SGX_MODE ${SGX_MODE} is not Debug, PreRelease or Release.")
endif()
set(ENCLAVE_INC_DIRS "${SGX_INCLUDE_DIR}" "${SGX_TLIBC_INCLUDE_DIR}" "${SGX_LIBCXX_INCLUDE_DIR}")
set(ENCLAVE_C_FLAGS "${SGX_COMMON_CFLAGS} -nostdinc -fvisibility=hidden -fpie -fstack-protector-strong")
set(ENCLAVE_CXX_FLAGS "${ENCLAVE_C_FLAGS} -nostdinc++")
set(APP_INC_DIRS "${SGX_PATH}/include")
set(APP_C_FLAGS "${SGX_COMMON_CFLAGS} -fPIC -Wno-attributes ")
set(APP_CXX_FLAGS "${APP_C_FLAGS}")
function(_build_edl_obj edl edl_search_paths use_prefix)
set(oneValueArgs EXTRA_FLAGS)
set(multiValueArgs EXTRA_INCLUDE_DIRS)
cmake_parse_arguments(OBJ "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
get_filename_component(EDL_NAME ${edl} NAME_WE)
get_filename_component(EDL_ABSPATH ${edl} ABSOLUTE)
set(EDL_T_C "${CMAKE_CURRENT_BINARY_DIR}/${EDL_NAME}_t.c")
set(SEARCH_PATHS "")
foreach(path ${edl_search_paths})
get_filename_component(ABSPATH ${path} ABSOLUTE)
list(APPEND SEARCH_PATHS "${ABSPATH}")
endforeach()
list(APPEND SEARCH_PATHS "${SGX_PATH}/include")
string(REPLACE ";" ":" SEARCH_PATHS "${SEARCH_PATHS}")
if(${use_prefix})
set(USE_PREFIX "--use-prefix")
endif()
if(NOT "${OBJ_EXTRA_INCLUDE_DIRS}" STREQUAL "")
# message("OBJ_EXTRA_INCLUDE_DIRS: ${OBJ_EXTRA_INCLUDE_DIRS}")
endif()
add_custom_command(OUTPUT ${EDL_T_C}
COMMAND ${SGX_EDGER8R} ${USE_PREFIX} --trusted ${EDL_ABSPATH} --search-path ${SEARCH_PATHS}
MAIN_DEPENDENCY ${EDL_ABSPATH}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set( LIB_ENCLAVE_C_FLAGS "-std=c11 ${ENCLAVE_C_FLAGS} ${OBJ_EXTRA_FLAGS}")
add_library(${target}-edlobj OBJECT ${EDL_T_C})
set_target_properties(${target}-edlobj PROPERTIES COMPILE_FLAGS ${LIB_ENCLAVE_C_FLAGS})
target_include_directories(${target}-edlobj PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ENCLAVE_INC_DIRS} ${OBJ_EXTRA_INCLUDE_DIRS})
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/${EDL_NAME}_t.h")
endfunction()
# build enclave shared library
function(add_enclave_library target)
set(optionArgs USE_PREFIX)
set(oneValueArgs EDL LDSCRIPT OPENSSL_LIBRARY_PATH WOLFSSL_LIBRARY_PATH)
set(multiValueArgs SRCS TRUSTED_LIBS EDL_SEARCH_PATHS EXTRA_INCLUDE_DIRS EXTRA_OPTIONS_FLAGS)
cmake_parse_arguments("SGX" "${optionArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if("${SGX_EDL}" STREQUAL "")
message(FATAL_ERROR "${target}: SGX enclave edl file is not provided!")
endif()
if(NOT "${SGX_EXTRA_INCLUDE_DIRS}" STREQUAL "")
# message("SGX_EXTRA_INCLUDE_DIRS: ${SGX_EXTRA_INCLUDE_DIRS}")
endif()
if(NOT "${SGX_EXTRA_OPTIONS_FLAGS}" STREQUAL "")
# message("SGX_EXTRA_OPTIONS_FLAGS: ${SGX_EXTRA_OPTIONS_FLAGS}")
endif()
if("${SGX_EDL_SEARCH_PATHS}" STREQUAL "")
message("${target}: SGX enclave edl file search paths are not provided!")
endif()
if(NOT "${SGX_LDSCRIPT}" STREQUAL "")
# message("SGX_LDSCRIPT provided ${SGX_LDSCRIPT}")
get_filename_component(LDS_ABSPATH ${SGX_LDSCRIPT} ABSOLUTE)
set(LDSCRIPT_FLAG "-Wl,--version-script=${LDS_ABSPATH}")
endif()
if(NOT "${SGX_OPENSSL_LIBRARY_PATH}" STREQUAL "")
# message("SGX_OPENSSL_LIBRARY_PATH provided ${SGX_OPENSSL_LIBRARY_PATH}")
if(SGX_MODE STREQUAL "Debug")
set(OPENSSL_SGX_SSL_LIBRARY_NAME "sgx_tsgxssld")
set(OPENSSL_SGX_CRYPTO_LIBRARY_NAME "sgx_tsgxssl_cryptod")
else()
set(OPENSSL_SGX_SSL_LIBRARY_NAME "sgx_tsgxssl")
set(OPENSSL_SGX_CRYPTO_LIBRARY_NAME "sgx_tsgxssl_crypto")
endif()
set(OPENSSL_FLAGS "-L${SGX_OPENSSL_LIBRARY_PATH} -Wl,--whole-archive -l${OPENSSL_SGX_SSL_LIBRARY_NAME} -Wl,--no-whole-archive -l${OPENSSL_SGX_CRYPTO_LIBRARY_NAME}")
endif()
if(NOT "${SGX_WOLFSSL_LIBRARY_PATH}" STREQUAL "")
# message("SGX_WOLFSSL_LIBRARY_PATH provided ${SGX_WOLFSSL_LIBRARY_PATH}")
set(WOLFSSL_SGX_LIBRARY_NAME "wolfssl.sgx.static.lib")
set(WOLFSSL_FLAGS "-L${SGX_WOLFSSL_LIBRARY_PATH} -l${WOLFSSL_SGX_LIBRARY_NAME}")
# message("WOLFSSL_FLAGS provided ${WOLFSSL_FLAGS}")
endif()
_build_edl_obj(${SGX_EDL} "${SGX_EDL_SEARCH_PATHS}" ${SGX_USE_PREFIX} EXTRA_INCLUDE_DIRS ${SGX_EXTRA_INCLUDE_DIRS} EXTRA_FLAGS ${SGX_EXTRA_OPTIONS_FLAGS})
set( LIB_ENCLAVE_CPP_FLAGS "-std=c++11 ${ENCLAVE_CXX_FLAGS} ${SGX_EXTRA_OPTIONS_FLAGS}")
add_library(${target} SHARED ${SGX_SRCS} $<TARGET_OBJECTS:${target}-edlobj>)
set_target_properties(${target} PROPERTIES COMPILE_FLAGS ${LIB_ENCLAVE_CPP_FLAGS})
target_include_directories(${target} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ENCLAVE_INC_DIRS} ${SGX_EXTRA_INCLUDE_DIRS})
set(TLIB_LIST "")
foreach(TLIB ${SGX_TRUSTED_LIBS})
string(APPEND TLIB_LIST "$<TARGET_FILE:${TLIB}> ")
add_dependencies(${target} ${TLIB})
endforeach()
set(SGX_COMPILER_OPTIONS "${SGX_COMMON_CFLAGS} -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles \
${OPENSSL_FLAGS} ${WOLFSSL_FLAGS} \
-L${SGX_LIBRARY_PATH} -Wl,--whole-archive -l${SGX_TRTS_LIB} -Wl,--no-whole-archive \
-Wl,--start-group ${TLIB_LIST} -lsgx_tstdc -lsgx_tcxx -lsgx_pthread -lsgx_tkey_exchange -lsgx_tcrypto -l${SGX_TSVC_LIB} -Wl,--end-group \
-Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
-Wl,-pie,-eenclave_entry -Wl,--export-dynamic \
${LDSCRIPT_FLAG} -Wl,--defsym,__ImageBase=0")
# message("add_enclave_library SGX_COMPILER_OPTIONS: ${SGX_COMPILER_OPTIONS}")
target_link_libraries(${target} ${SGX_COMPILER_OPTIONS})
endfunction()
# sign the enclave, according to configurations one-step or two-step signing will be performed.
# default one-step signing output enclave name is target.signed.so, change it with OUTPUT option.
function(enclave_sign target)
set(optionArgs IGNORE_INIT IGNORE_REL)
set(oneValueArgs KEY CONFIG OUTPUT)
cmake_parse_arguments("SGX" "${optionArgs}" "${oneValueArgs}" "" ${ARGN})
if("${SGX_CONFIG}" STREQUAL "")
message("${target}: SGX enclave config is not provided!")
else()
get_filename_component(CONFIG_ABSPATH ${SGX_CONFIG} ABSOLUTE)
endif()
if("${SGX_KEY}" STREQUAL "")
if (NOT SGX_HW OR NOT SGX_MODE STREQUAL "Release")
message(FATAL_ERROR "${target}: Private key used to sign enclave is not provided!")
endif()
else()
get_filename_component(KEY_ABSPATH ${SGX_KEY} ABSOLUTE)
endif()
if("${SGX_OUTPUT}" STREQUAL "")
set(OUTPUT_NAME "${target}.signed.so")
else()
set(OUTPUT_NAME ${SGX_OUTPUT})
endif()
if(${SGX_IGNORE_INIT})
set(IGN_INIT "-ignore-init-sec-error")
endif()
if(${SGX_IGNORE_REL})
set(IGN_REL "-ignore-rel-error")
endif()
if(SGX_HW AND SGX_MODE STREQUAL "Release")
add_custom_target(${target}-sign ALL
COMMAND ${SGX_ENCLAVE_SIGNER} gendata
$<$<NOT:$<STREQUAL:${SGX_CONFIG},>>:-config> $<$<NOT:$<STREQUAL:${SGX_CONFIG},>>:${CONFIG_ABSPATH}>
-enclave $<TARGET_FILE:${target}> -out $<TARGET_FILE_DIR:${target}>/${target}_hash.hex ${IGN_INIT} ${IGN_REL}
COMMAND ${CMAKE_COMMAND} -E cmake_echo_color
--cyan "SGX production enclave first step signing finished, \
use ${CMAKE_CURRENT_BINARY_DIR}/${target}_hash.hex for second step"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
else()
add_custom_target(${target}-sign ALL ${SGX_ENCLAVE_SIGNER} sign -key ${KEY_ABSPATH}
$<$<NOT:$<STREQUAL:${SGX_CONFIG},>>:-config> $<$<NOT:$<STREQUAL:${SGX_CONFIG},>>:${CONFIG_ABSPATH}>
-enclave $<TARGET_FILE:${target}>
-out $<TARGET_FILE_DIR:${target}>/${OUTPUT_NAME}
${IGN_INIT} ${IGN_REL}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endif()
set(CLEAN_FILES "$<TARGET_FILE_DIR:${target}>/${OUTPUT_NAME};$<TARGET_FILE_DIR:${target}>/${target}_hash.hex")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_FILES}")
endfunction()
function(add_untrusted_executable target)
set(optionArgs USE_PREFIX)
set(oneValueArgs OPENSSL_LIBRARY_PATH)
set(multiValueArgs SRCS EDL EDL_SEARCH_PATHS EXTRA_BUILD_OBJECTS EXTRA_INCLUDE_DIRS EXTRA_OPTIONS_FLAGS)
cmake_parse_arguments("SGX" "${optionArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if("${SGX_EDL}" STREQUAL "")
message(FATAL_ERROR "${target}: SGX enclave edl file is not provided!")
endif()
if("${SGX_EDL_SEARCH_PATHS}" STREQUAL "")
message("${target}: SGX enclave edl file search paths are not provided!")
endif()
if(NOT "${SGX_OPENSSL_LIBRARY_PATH}" STREQUAL "")
message("SGX_OPENSSL_LIBRARY_PATH provided ${SGX_OPENSSL_LIBRARY_PATH}")
if(SGX_MODE STREQUAL "Debug")
set(OPENSSL_SGX_SSL_LIBRARY_NAME "sgx_usgxssld")
else()
set(OPENSSL_SGX_SSL_LIBRARY_NAME "sgx_usgxssl")
endif()
set(OPENSSL_FLAGS "-L${SGX_OPENSSL_LIBRARY_PATH} -Wl,--whole-archive -l${OPENSSL_SGX_SSL_LIBRARY_NAME} ")
message("OPENSSL_FLAGS provided ${OPENSSL_FLAGS}")
endif()
if(NOT "${SGX_EXTRA_INCLUDE_DIRS}" STREQUAL "")
message("SGX_EXTRA_INCLUDE_DIRS: ${SGX_EXTRA_INCLUDE_DIRS}")
endif()
if(NOT "${SGX_EXTRA_OPTIONS_FLAGS}" STREQUAL "")
message("SGX_EXTRA_OPTIONS_FLAGS: ${SGX_EXTRA_OPTIONS_FLAGS}")
endif()
set(EDL_U_SRCS "")
foreach(EDL ${SGX_EDL})
get_filename_component(EDL_NAME ${EDL} NAME_WE)
get_filename_component(EDL_ABSPATH ${EDL} ABSOLUTE)
set(EDL_U_C "${CMAKE_CURRENT_BINARY_DIR}/${EDL_NAME}_u.c")
set(EDL_U_H "${CMAKE_CURRENT_BINARY_DIR}/${EDL_NAME}_u.h")
set(SEARCH_PATHS "")
foreach(path ${SGX_EDL_SEARCH_PATHS})
get_filename_component(ABSPATH ${path} ABSOLUTE)
list(APPEND SEARCH_PATHS "${ABSPATH}")
endforeach()
list(APPEND SEARCH_PATHS "${SGX_PATH}/include")
string(REPLACE ";" ":" SEARCH_PATHS "${SEARCH_PATHS}")
if(${SGX_USE_PREFIX})
set(USE_PREFIX "--use-prefix")
endif()
add_custom_command(OUTPUT ${EDL_U_C}
COMMAND ${SGX_EDGER8R} ${USE_PREFIX} --untrusted ${EDL_ABSPATH} --search-path ${SEARCH_PATHS}
MAIN_DEPENDENCY ${EDL_ABSPATH}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
list(APPEND EDL_U_SRCS ${EDL_U_C})
list(APPEND EDL_U_HDRS ${EDL_U_H})
endforeach()
set(EXTRA_BUILD_OBJECTS "")
foreach(EBOBJ ${SGX_EXTRA_BUILD_OBJECTS})
get_filename_component(EBOBJ_NAME ${EBOBJ} NAME_WE)
get_filename_component(EBOBJ_ABSPATH ${EBOBJ} ABSOLUTE)
set(EBOBJ_NAME "${EBOBJ_NAME}-ext-obj")
add_library(${EBOBJ_NAME} OBJECT IMPORTED GLOBAL)
set_target_properties(${EBOBJ_NAME} PROPERTIES IMPORTED_OBJECTS ${EBOBJ_ABSPATH})
list(APPEND EXTRA_BUILD_OBJECTS ${EBOBJ_NAME})
endforeach()
message("EXTRA_BUILD_OBJECTS ====== ${EXTRA_BUILD_OBJECTS}")
add_executable(${target} ${SGX_SRCS} ${EDL_U_SRCS} $<TARGET_OBJECTS:${EXTRA_BUILD_OBJECTS}>)
set( U_EXEC_CPP_FLAGS "-std=c++17 ${APP_CXX_FLAGS} ${SGX_EXTRA_OPTIONS_FLAGS} ")
message("U_EXEC_CPP_FLAGS : ${U_EXEC_CPP_FLAGS}")
set_target_properties(${target} PROPERTIES COMPILE_FLAGS ${U_EXEC_CPP_FLAGS})
target_include_directories(${target} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${SGX_EXTRA_INCLUDE_DIRS} ${APP_INC_DIRS})
target_link_libraries(${target} "${SGX_COMMON_CFLAGS} \
${OPENSSL_FLAGS} \
-L${SGX_LIBRARY_PATH} \
-l${SGX_URTS_LIB} \
-l${SGX_USVC_LIB} \
-lcrypto -lwolfssl -lpthread")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${EDL_U_HDRS})
endfunction()
else(SGX_FOUND)
message(WARNING "Intel SGX SDK not found!")
if(SGX_FIND_REQUIRED)
message(FATAL_ERROR "Could NOT find Intel SGX SDK!")
endif()
endif(SGX_FOUND)
that was called as
add_enclave_library(Enclave
SRCS ${T_SRCS} ${T_SRCS_Crypto} ${T_SRCS_HTTPS_Handlers} ${T_SRCS_DataUtils} ${T_SRCS_Agg}
EDL Enclave/Enclave.edl
EDL_SEARCH_PATHS ${EDL_SEARCH_PATHS}
USE_PREFIX
LDSCRIPT ${LDS}
OPENSSL_LIBRARY_PATH ${SGX_OPENSSL_LIB_DIR}
WOLFSSL_LIBRARY_PATH ${SGX_WOLFSSL_LIB_DIR}
EXTRA_INCLUDE_DIRS ${T_INCLUDE_DIRS}
EXTRA_OPTIONS_FLAGS ${WOLFSSL_ENCLAVE_EXTRA_FLAGS})
enclave_sign(Enclave KEY Enclave/Enclave_private.pem CONFIG Enclave/Enclave.config.xml)
set(WOLFSSL_APP_EXTRA_FLAGS "${WOLFSSL_C_EXTRA_FLAGS} -DWOLFSSL_SHA512 -DOPENSSL_EXTRA -DOPENSSL_ALL -DLWS_HAVE_SSL_CTX_load_verify_file \
-DLWS_WITH_SOCKS5 -DLWS_WITH_DETAILED_LATENCY -DLWS_WITH_SPAWN -DLWS_WITH_RANGES -DLWS_WITH_ACCESS_LOG -DCPPHTTPLIB_OPENSSL_SUPPORT -DHAVE_SUPPORTED_CURVES")
add_untrusted_executable(cscore SRCS ${U_SRCS} ${U_SRCS_Exchanges}
EDL Enclave/Enclave.edl
EDL_SEARCH_PATHS ${EDL_SEARCH_PATHS}
OPENSSL_LIBRARY_PATH ${SGX_OPENSSL_LIB_DIR}
EXTRA_INCLUDE_DIRS ${U_INCLUDE_DIRS}
EXTRA_OPTIONS_FLAGS ${WOLFSSL_APP_EXTRA_FLAGS}
EXTRA_BUILD_OBJECTS App/kdb/c.o
)
add_dependencies(cscore Enclave-sign)
Any help is appreciated.

Compiling cpputest with mingw-w64

I'm trying to compile cpputest with mingw-w64 but can't make it work and I can't find any resources on how to do that. So hopefully this question will help others in the future.
I would like to compile cpputest using mingw-w64, preferably without installing MSYS2 or other packages.
I have mingw-w64 i686-8.1.0-posix-dwarf-rt_v6-rev0 installed. I've cloned cpputest from here.
I tried following Compiling Google test with Mingw-w64 and Compiling and using CppUTest 3.8 under MSYS2/MinGW32 by calling CMake with:
cmake ^
-G "MinGW Makefiles" ^
-D CMAKE_C_COMPILER=gcc.exe ^
-D CMAKE_CXX_COMPILER=g++.exe ^
-D CMAKE_MAKE_PROGRAM=mingw32-make.exe ^
-D C++11=ON ^
.
Which yields:
-------------------------------------------------------
CppUTest Version 3.8
Current compiler options:
CC: C:/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gcc.exe
CXX: C:/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/g++.exe
CppUTest CFLAGS: -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorMallocMacros.h" -Wall -Wextra -pedantic -Wshadow -Wswitch-default -Wswitch-enum -Wconversion -Wsign-conversion -Wno-padded -Wno-long-long -Wstrict-prototypes
CppUTest CXXFLAGS: -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorNewMacros.h" -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorMallocMacros.h" -Wall -Wextra -pedantic -Wshadow -Wswitch-default -Wswitch-enum -Wconversion -Wsign-conversion -Wno-padded -Wno-long-long -Woverloaded-virtual -Wno-old-style-cast -Wno-c++14-compat
CppUTest LDFLAGS:
Features configured in CppUTest:
Memory Leak Detection: ON
Compiling Extensions: ON
Support Long Long: OFF
Use CppUTest flags: ON
Using Standard C library: ON
Using Standard C++ library: ON
Using C++11 library: ON
Generating map file: OFF
Compiling with coverage: OFF
Compile and run self-tests ON
Run self-tests separately OFF
-------------------------------------------------------
Running make fails with:
>mingw32-make.exe
Scanning dependencies of target CppUTest
[ 1%] Building CXX object src/CppUTest/CMakeFiles/CppUTest.dir/CommandLineArguments.cpp.obj
In file included from C:/git/tdd/cpputest/include/CppUTest/Utest.h:34,
from C:/git/tdd/cpputest/include/CppUTest/TestHarness.h:39,
from C:\git\tdd\cpputest\src\CppUTest\CommandLineArguments.cpp:29:
C:/git/tdd/cpputest/include/CppUTest/SimpleString.h:183:31: error: 'nullptr_t' does not name a type
SimpleString StringFrom(const nullptr_t value);
^~~~~~~~~
C:/git/tdd/cpputest/include/CppUTest/SimpleString.h:183:31: note: 'nullptr_t' is defined in header '<cstddef>'; did you forget to '#include <cstddef>'?
C:/git/tdd/cpputest/include/CppUTest/SimpleString.h:42:1:
+#include <cstddef>
C:/git/tdd/cpputest/include/CppUTest/SimpleString.h:183:31:
SimpleString StringFrom(const nullptr_t value);
^~~~~~~~~
mingw32-make.exe[2]: *** [src\CppUTest\CMakeFiles\CppUTest.dir\build.make:63: src/CppUTest/CMakeFiles/CppUTest.dir/CommandLineArguments.cpp.obj] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:886: src/CppUTest/CMakeFiles/CppUTest.dir/all] Error 2
mingw32-make.exe: *** [Makefile:140: all] Error 2
I tried the obvious solution of adding cstddef to SimpleString.h:
--- a/include/CppUTest/SimpleString.h
+++ b/include/CppUTest/SimpleString.h
## -180,6 +180,9 ## SimpleString BracketsFormattedHexString(SimpleString hexString);
* Specifically nullptr_t is not officially supported
*/
#if __cplusplus > 199711L && !defined __arm__
+
+#include <cstddef>
+
SimpleString StringFrom(const nullptr_t value);
#endif
But that still fails with the same error.
So I tried following Build error with CMake and MSYS2 mingw-w64 by installing MSYS2 and then mingw-w64, CMake and Ninja. That gives:
-------------------------------------------------------
CppUTest Version 3.8
Current compiler options:
CC: C:/msys64/mingw64/bin/cc.exe
CXX: C:/msys64/mingw64/bin/c++.exe
CppUTest CFLAGS: -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorMallocMacros.h" -Wall -Wextra -pedantic -Wshadow -Wswitch-default -Wswitch-enum -Wconversion -Wsign-conversion -Wno-padded -Wno-long-long -Wstrict-prototypes
CppUTest CXXFLAGS: -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorNewMacros.h" -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorMallocMacros.h" -Wall -Wextra -pedantic -Wshadow -Wswitch-default -Wswitch-enum -Wconversion -Wsign-conversion -Wno-padded -Wno-long-long -Woverloaded-virtual -Wno-old-style-cast
CppUTest LDFLAGS:
Features configured in CppUTest:
Memory Leak Detection: ON
Compiling Extensions: ON
Support Long Long: OFF
Use CppUTest flags: ON
Using Standard C library: ON
Using Standard C++ library: ON
Using C++11 library: OFF
Generating map file: OFF
Compiling with coverage: OFF
Compile and run self-tests ON
Run self-tests separately OFF
-------------------------------------------------------
Compiling with cmake -G Ninja . && ninja fails with:
include/CppUTest/SimpleString.h:183:31:
SimpleString StringFrom(const nullptr_t value);
^~~~~~~~~
[10/98] Building CXX object src/CppUTest/CMakeFiles/CppUTest.dir/TestMemoryAllocator.cpp.obj
FAILED: src/CppUTest/CMakeFiles/CppUTest.dir/TestMemoryAllocator.cpp.obj
C:\msys64\mingw64\bin\c++.exe -DCPPUTEST_HAVE_STRDUP=1 -DHAVE_CONFIG_H -D_TIMESPEC_DEFINED=1 -I. -Iinclude -Isrc/CppUTest/../../include -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorNewMacros.h" -include "C:/git/tdd/cpputest/include/CppUTest/MemoryLeakDetectorMallocMacros.h" -Wall -Wextra -pedantic -Wshadow -Wswitch-default -Wswitch-enum -Wconversion -Wsign-conversion -Wno-padded -Wno-long-long -Woverloaded-virtual -Wno-old-style-cast -O2 -g -DNDEBUG -MD -MT src/CppUTest/CMakeFiles/CppUTest.dir/TestMemoryAllocator.cpp.obj -MF src\CppUTest\CMakeFiles\CppUTest.dir\TestMemoryAllocator.cpp.obj.d -o src/CppUTest/CMakeFiles/CppUTest.dir/TestMemoryAllocator.cpp.obj -c src/CppUTest/TestMemoryAllocator.cpp
In file included from include/CppUTest/Utest.h:34,
from include/CppUTest/TestHarness.h:39,
from src/CppUTest/TestMemoryAllocator.cpp:28:
include/CppUTest/SimpleString.h:183:31: error: 'nullptr_t' does not name a type
SimpleString StringFrom(const nullptr_t value);
^~~~~~~~~
include/CppUTest/SimpleString.h:183:31: note: 'nullptr_t' is defined in header '<cstddef>'; did you forget to '#include <cstddef>'?
include/CppUTest/SimpleString.h:42:1:
+#include <cstddef>
include/CppUTest/SimpleString.h:183:31:
SimpleString StringFrom(const nullptr_t value);
^~~~~~~~~
ninja: build stopped: subcommand failed.
Thanks!!
Turns out this was a bug in CppUtest.
## -562,7 +562,7 ## SimpleString BracketsFormattedHexString(SimpleString hexString)
* Specifically nullptr_t is not officially supported
*/
#if __cplusplus > 199711L && !defined __arm__
- SimpleString StringFrom(const nullptr_t value)
+ SimpleString StringFrom(const std::nullptr_t value)
{
(void) value;
return "(null)";
It's fixed since Dec. 5th, 2019.
https://github.com/leonardopsantos/cpputest/commit/cb8c457dda6741ede7009103db99967b5f27c969

including itensor library in my cmake project in clion

so I am very new to CLion and CMake, so sorry in advance for wrong usage of terminology. I am suffering the following problem:
In my project I want to include the ITensor library which is essentially a non-CMake project. I cloned the git to my computer and build the ITensor project. Next I wanted to use it in another project linking against it with CMake:
My Code in main.cpp:
#include <iostream>
#include "itensor.h"
int main() {
std::string some_string = "Hello world";
return 0;
}
and my CMakeLists.txt looks like:
cmake_minimum_required(VERSION 3.6)
project(tutorial)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(ITENSOR_DIR PATH/TO/ITENSOR)
include_directories(ITENSOR_DIR/itensor)
set(SOURCE_FILES
main.cpp
${ITENSOR_DIR}/itensor/itensor.h
${ITENSOR_DIR}/itensor/itensor.cc)
add_executable(tutorial ${SOURCE_FILES})
Unfortunately, the project 'tutorial' does not build in CLion. Likewise, CLion cannot resolve the dependency itensor.h.
Anybody an Idea for why this is, respectively how to fix it?
After trying Thomas5631's solution the compilation ran into linking issues with lapack. I solved this by adding some flags, though I'm not sure if all of them are required.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(my_project)
#Bring the headers into the project (full or relative path)
include_directories(itensor)
#Link the Itensor library
add_library(itensor STATIC IMPORTED)
set_property(TARGET itensor PROPERTY IMPORTED_LOCATION /home/david/my_project/itensor/lib/libitensor.a)
#Set a variable with all the new flags
set(ITENSOR_FLAGS "-DPLATFORM_lapack -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0")
set(ITENSOR_LINK_FLAGS "-DPLATFORM_lapack -L/home/david/my_project/itensor/lib -litensor -lpthread -L/usr/lib -lblas -llapack")
#Append the new flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${ITENSOR_FLAGS}")
add_executable(my_project main.cpp)
target_link_libraries(my_project itensor "${ITENSOR_LINK_FLAGS}")
Motivation: In the folder itensor/project_template there is a sample program which is simple enough to compile with make (from the terminal). The output of the compilation reveals the flags:
g++ -m64 -std=c++11 -c -I. -I/home/david/my_project/itensor -I/usr/include -O3 -DNDEBUG -Wall -DPLATFORM_lapack -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -Wno-unused-variable -o myappname.o myappname.cc
[... some warnings ...]
g++ -m64 -std=c++11 -c -I. -I/home/david/my_project/itensor -I/usr/include -O3 -DNDEBUG -Wall -DPLATFORM_lapack -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -Wno-unused-variable -o myclass.o myclass.cc
g++ -m64 -std=c++11 -I. -I/home/david/my_project/itensor -I/usr/include -O3 -DNDEBUG -Wall -DPLATFORM_lapack -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -Wno-unused-variable myappname.o myclass.o -o myappname -L/home/david/my_project/itensor/lib -litensor -lpthread -L/usr/lib -lblas -llapack
I got around the issue with the following main.cpp:
#include <iostream>
#include "itensor/itensor.h"
int main() {
std::string some_string = "Hello world";
return 0;
}
And the following CMakeLists.txt:
project(tutorial)
cmake_minimum_required(VERSION 3.6)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
#Bring the headers into the project
include_directories(/home/tom/Documents/workspace/ITensor/)
#Link the library
add_library(itensor STATIC IMPORTED)
set_property(TARGET itensor PROPERTY IMPORTED_LOCATION /home/tom/Documents/workspace/ITensor/lib/libitensor.a)
set(SOURCE_FILES main.cpp)
add_executable(tutorial ${SOURCE_FILES})
Where the path to ITensor can be either relative (using the ${PROJECT_SOURCE_DIR} variable) or absolute as I have shown.

undefined reference to `vtable in custom lib created with cmake

I'm getting the error: lib/libhrlLib.so: undefined reference to `hrlQseqDev::waiting(bool, int)' and some more ...
I'm trying to build my project with cmake (3.7.2) instead of with qmake (Qt5)
CMakeLists.txt:
project(${TARGET_NAME})
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 14)
set(POSITION_INDEPENDENT_CODE FALSE)
macro(NAMELIST erg in)
set(os "")
set(srcs ${in})
separate_arguments(srcs)
foreach(is ${srcs})
set(os ${os} ${is})
endforeach(is)
set(${erg} ${os})
endmacro(NAMELIST)
macro(FINDMODULES erg in)
set(os "")
set(srcs ${in})
separate_arguments(srcs)
foreach(is ${srcs})
find_package(Qt5${is} REQUIRED)
set(os ${os} Qt5::${is})
endforeach(is)
set(${erg} ${os})
endmacro(FINDMODULES)
NAMELIST(SRCS ${TARGET_SRCS})
FINDMODULES(QLIBS ${QMODULES})
if(INC_PATH)
NAMELIST(INCS ${INC_PATH})
set(INCLUDES ${INCS})
endif(INC_PATH)
if(TARGET_EXTLIBS)
NAMELIST(EXTRA_LIBS ${EXTRA_LIBS} "${TARGET_EXTLIBS}")
endif(TARGET_EXTLIBS)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
include_directories(${INCLUDES})
if(BUILD_LIB)
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-shared")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "-shared")
add_library(${TARGET_NAME} SHARED ${SRCS})
target_link_libraries(${TARGET_NAME} ${QLIBS} ${EXTRA_LIBS})
set_property(TARGET ${TARGET_NAME} PROPERTY VERSION "1.0.0")
set_property(TARGET ${TARGET_NAME} PROPERTY SOVERSION 1 )
install(TARGETS ${TARGET_NAME} LIBRARY DESTINATION lib)
install(TARGETS ${TARGET_NAME} LIBRARY DESTINATION lib NAMELINK_ONLY)
else()
if(USE_LIB_PATH)
add_executable(${TARGET_NAME} ${SRCS})
target_link_libraries(${TARGET_NAME} -L${OUT_PATH}/lib lib${USE_LIB}.so.1
${QLIBS} ${EXTRA_LIBS})
else()
add_executable(${TARGET_NAME} ${SRCS})
target_link_libraries(${TARGET_NAME} ${QLIBS} ${EXTRA_LIBS})
endif(USE_LIB_PATH)
install(TARGETS ${TARGET_NAME} RUNTIME DESTINATION bin)
endif(BUILD_LIB)
The shared library is built with:
cmake -G "Unix Makefiles" -DINC_PATH:STRING="some includes" -DTARGET_EXTLIBS:STRING="sys libs" -DTARGET_NAME:STRING=LibName -DBUILD_LIB:BOOL=1 -DTARGET_SRCS:STRING="cpp- and c-file" -DQMODULES:STRING="Core Gui Widgets PrintSupport" -DOUT_PATH:STRING=InstallPath .. -DCMAKE_INSTALL_PREFIX=InstallPath >> /dev/null
When I try to build a program against this shared library with:
cmake -G "Unix Makefiles" -DINC_PATH:STRING="some includes" -DTARGET_EXTLIBS:STRING="sys libs" -DTARGET_NAME:STRING=ProgName -DBUILD_LIB:BOOL=0 -DTARGET_SRCS:STRING="cpp-file" -DQMODULES:STRING="Core Gui Widgets PrintSupport" -DOUT_PATH=InstallPath -DUSE_LIB:STRING="LibName" -DUSE_LIB_PATH:STRING="BuildPath of LibName" .. -DCMAKE_INSTALL_PREFIX=InstallPath >> /dev/null
I get the error 'lib/libhrlLib.so: undefined reference to ...'.
I do not get this error when the library was built with qmake.
How can I fix this?