Как кросс-компилировать проекты C ++ DCMTK с помощью CMake (от Ubuntu до Windows) - PullRequest
0 голосов
/ 07 апреля 2020

Я пытаюсь кросс-компилировать самый простой пример DCMTK, который я нашел, и я могу его собрать. Я использую Ubuntu 18.04.4 LTS для кросс-компиляции с использованием x86_64-w64-mingw32.

Я также проверил эти ответы и примеры, но у меня они не работали:

Это единственный файл проекта (основной. cpp):

#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"

using namespace std;
int main()
{
    DcmFileFormat fileformat;
    OFCondition status = fileformat.loadFile("sample.dcm");
    if (status.good())
    {
        OFString patientName;
        if (fileformat.getDataset()->findAndGetOFString(DCM_PatientName, patientName).good())
        {
            cout << "Patient's Name: " << patientName << endl;
        } else
            cerr << "Error: cannot access Patient's Name!" << endl;

        OFString patientID;
        if (fileformat.getDataset()->findAndGetOFString(DCM_PatientID, patientID).good())
        {
            cout << "Patient's ID: " << patientID << endl;
        } else
            cerr << "Fatal Error: cannot access Patient's ID!" << endl;

    } else
        cerr << "Error: cannot read DICOM file (" << status.text() << ")" << endl;
    return 0;
}

Это мой файл конфигурации CMake.txt:

cmake_minimum_required(VERSION 2.8)
PROJECT(test1DCMTK)

add_executable(${PROJECT_NAME} "main.cpp")

SET(DCMTK_DIR /usr/local/include/dcmtk)

ADD_DEFINITIONS(-D_REENTRANT)

INCLUDE_DIRECTORIES(${DCMTK_DIR}/include)
LINK_DIRECTORIES(${DCMTK_DIR}/lib)

TARGET_LINK_LIBRARIES(test1DCMTK ofstd dcmdata)

И я использую этот файл набора инструментов:

### CMAKE cross compile settings for Windows running from Ubuntu 18.04.4 LTS x86_x64 amd64 ###

# the name of the target operating system
SET(CMAKE_SYSTEM_NAME Windows)
SET(TOOLCHAIN_PREFIX x86_64-w64-mingw32)

SET(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc-posix)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++-posix)
#SET(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

# which compilers to use for C and C++
#SET(CMAKE_C_COMPILER x86_x64-mingw32-gcc)
#SET(CMAKE_CXX_COMPILER x86_x64-mingw32-g++)
###SET(CMAKE_RC_COMPILER i586-mingw32msvc-windres)

# here is the target environment located
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} /usr/lib/gcc/${TOOLCHAIN_PREFIX}/7.3-posix)

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Когда я настраиваю с помощью CMake, я получаю это:

The C compiler identification is GNU 7.3.0
The CXX compiler identification is GNU 7.3.0
Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc-posix
Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc-posix -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++-posix
Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++-posix -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
Configuring done

Но когда я пытаюсь собрать код, вот что я получаю:

/home/yo/Projects/test1DCMTK/main.cpp:1:10: fatal error: dcmtk/config/osconfig.h: No existe el archivo o el directorio
 #include "dcmtk/config/osconfig.h"
          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/test1DCMTK.dir/build.make:63: fallo en las instrucciones para el objetivo 'CMakeFiles/test1DCMTK.dir/main.cpp.obj'
make[2]: *** [CMakeFiles/test1DCMTK.dir/main.cpp.obj] Error 1
CMakeFiles/Makefile2:67: fallo en las instrucciones para el objetivo 'CMakeFiles/test1DCMTK.dir/all'
make[1]: *** [CMakeFiles/test1DCMTK.dir/all] Error 2
Makefile:83: fallo en las instrucciones para el objetivo 'all'
make: *** [all] Error 2

Однако библиотека существует:

(base) yo@MiPC:/usr/local/include/dcmtk/config$ ls /usr/local/include/dcmtk/config/os*
/usr/local/include/dcmtk/config/osconfig.h

Итак, что я делаю не так? Что я должен сделать, чтобы заставить это работать должным образом?

Я попробовал ответ @squareskittles, и я получил это, когда попытался построить:

(base) yo@MiPC:~/Projects/build-test1DCMTK-win32$ make all
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yo/Projects/build-test1DCMTK-win32
Scanning dependencies of target test1DCMTK
[ 50%] Building CXX object CMakeFiles/test1DCMTK.dir/main.cpp.obj
In file included from /usr/local/include/dcmtk/dcmdata/dcistrma.h:28:0,
                 from /usr/local/include/dcmtk/dcmdata/dctk.h:30,
                 from /home/elekta/Projects/QT/TUTOS-QT/test1DCMTK/main.cpp:2:
/usr/local/include/dcmtk/ofstd/offile.h:74:9: error: ‘fpos64_t’ does not name a type; did you mean ‘fpos_t’?
 typedef fpos64_t offile_fpos_t;
         ^~~~~~~~
         fpos_t
/usr/local/include/dcmtk/ofstd/offile.h:866:15: error: ‘offile_fpos_t’ has not been declared
   int fgetpos(offile_fpos_t *pos)
               ^~~~~~~~~~~~~
/usr/local/include/dcmtk/ofstd/offile.h:886:15: error: ‘offile_fpos_t’ has not been declared
   int fsetpos(offile_fpos_t *pos)
               ^~~~~~~~~~~~~
/usr/local/include/dcmtk/ofstd/offile.h: In member function ‘int OFFile::fseek(offile_off_t, int)’:
/usr/local/include/dcmtk/ofstd/offile.h:757:5: error: ‘offile_fpos_t’ was not declared in this scope
     offile_fpos_t off2 = off;
     ^~~~~~~~~~~~~
/usr/local/include/dcmtk/ofstd/offile.h:757:5: note: suggested alternative: ‘offile_off_t’
     offile_fpos_t off2 = off;
     ^~~~~~~~~~~~~
     offile_off_t
/usr/local/include/dcmtk/ofstd/offile.h:792:9: error: ‘off2’ was not declared in this scope
         off2 += buf.st_size;
         ^~~~
/usr/local/include/dcmtk/ofstd/offile.h:792:9: note: suggested alternative: ‘off’
         off2 += buf.st_size;
         ^~~~
         off
/usr/local/include/dcmtk/ofstd/offile.h:808:29: error: ‘off2’ was not declared in this scope
     result = this->fsetpos(&off2);
                             ^~~~
/usr/local/include/dcmtk/ofstd/offile.h:808:29: note: suggested alternative: ‘off’
     result = this->fsetpos(&off2);
                             ^~~~
                             off
/usr/local/include/dcmtk/ofstd/offile.h: In member function ‘offile_off_t OFFile::ftell()’:
/usr/local/include/dcmtk/ofstd/offile.h:837:5: error: ‘offile_fpos_t’ was not declared in this scope
     offile_fpos_t pos;
     ^~~~~~~~~~~~~
/usr/local/include/dcmtk/ofstd/offile.h:837:5: note: suggested alternative: ‘offile_off_t’
     offile_fpos_t pos;
     ^~~~~~~~~~~~~
     offile_off_t
/usr/local/include/dcmtk/ofstd/offile.h:838:24: error: ‘pos’ was not declared in this scope
     if (this->fgetpos(&pos) != 0)
                        ^~~
/usr/local/include/dcmtk/ofstd/offile.h:838:24: note: suggested alternative: ‘pow’
     if (this->fgetpos(&pos) != 0)
                        ^~~
                        pow
/usr/local/include/dcmtk/ofstd/offile.h:843:12: error: ‘pos’ was not declared in this scope
     return pos;
            ^~~
/usr/local/include/dcmtk/ofstd/offile.h:843:12: note: suggested alternative: ‘pow’
     return pos;
            ^~~
            pow
/usr/local/include/dcmtk/ofstd/offile.h: In member function ‘int OFFile::fgetpos(int*)’:
/usr/local/include/dcmtk/ofstd/offile.h:873:48: error: cannot convert ‘int*’ to ‘fpos_t* {aka long long int*}’ for argument ‘2’ to ‘int fgetpos(FILE*, fpos_t*)’
     result = STDIO_NAMESPACE fgetpos(file_, pos);
                                                ^
/usr/local/include/dcmtk/ofstd/offile.h: In member function ‘int OFFile::fsetpos(int*)’:
/usr/local/include/dcmtk/ofstd/offile.h:893:48: error: cannot convert ‘int*’ to ‘const fpos_t* {aka const long long int*}’ for argument ‘2’ to ‘int fsetpos(FILE*, const fpos_t*)’
     result = STDIO_NAMESPACE fsetpos(file_, pos);
                                                ^
CMakeFiles/test1DCMTK.dir/build.make:63: fallo en las instrucciones para el objetivo 'CMakeFiles/test1DCMTK.dir/main.cpp.obj'
make[2]: *** [CMakeFiles/test1DCMTK.dir/main.cpp.obj] Error 1
CMakeFiles/Makefile2:67: fallo en las instrucciones para el objetivo 'CMakeFiles/test1DCMTK.dir/all'
make[1]: *** [CMakeFiles/test1DCMTK.dir/all] Error 2
Makefile:83: fallo en las instrucciones para el objetivo 'all'
make: *** [all] Error 2

1 Ответ

1 голос
/ 07 апреля 2020

Ваш CMake добавляет dcmtk включающие каталоги, подобные этому:

SET(DCMTK_DIR /usr/local/include/dcmtk)
INCLUDE_DIRECTORIES(${DCMTK_DIR}/include)

, который оценивает этот путь:

/usr/local/include/dcmtk/include

Ваш main.cpp файл затем включает dcmtk/config/osconfig.h , который (объединяя два пути) предполагает, что заголовок расположен здесь:

/usr/local/include/dcmtk/include/dcmtk/config/osconfig.h

Это не кажется правильным. Попробуйте вместо этого включить только /usr/local/include в файл CMake:

INCLUDE_DIRECTORIES(/usr/local/include)

В качестве примечания, CMake предоставляет Модуль поиска для DCMTK, поэтому он попытается найти библиотеку и установить для вас переменную . Вы можете попробовать что-то вроде этого, но в зависимости от вашей версии CMake у вас могут быть разные результаты:

cmake_minimum_required(VERSION 2.8)
PROJECT(test1DCMTK)

find_package(DCMTK REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

SET(DCMTK_DIR /usr/local/include/dcmtk)

ADD_DEFINITIONS(-D_REENTRANT)

# Use the INCLUDE_DIRS variable populated by the DCMTK find module.
INCLUDE_DIRECTORIES(${DCMTK_INCLUDE_DIRS})

# Use the LIBRARIES variable populated by the DCMTK find module.
TARGET_LINK_LIBRARIES(test1DCMTK ofstd ${DCMTK_LIBRARIES})

Если это успешно найдет DCMTK, DCMTK_INCLUDE_DIRS, скорее всего, будет содержать все необходимые каталоги включения для DCMTK, а не только каталог включения, который вы добавили вручную. Это может помочь устранить ошибки компилятора, которые вы видите.

...