(G)When developing an RTC with multithread (/MT) and multithreading debug (/MTd) specified in the runtime library in Visual Studio, if the executable file of /MT, /MTd links with another DLL and processes it, heap It may end abnormally due to destruction or the like. If you want to specify multithreading (/MT) and multithreading debugging (/MTd), create a static library of OpenRTM-aist as follows.
Please obtain the OpenRTM-aist source code from the following.
Please download to appropriate place after downloading. In this example, place it in the following position.
C:\openrtm_work\OpenRTM-aist
Please obtain binary package of omniORB from the following. Please select the one appropriate for the version of Visual Studio.
After downloading, please create and expand a folder named omniORB in the OpenRTM-aist folder.
C:\openrtm_work\OpenRTM-aist\omniORB
/MT, /MTd Edits various files for building.
Edit the following batch file called build.bat.
C:\openrtm_work\OpenRTM-aist\build.bat
Please change the following places as appropriate depending on the environment.
set RTM_ROOT=%~dp0 set COIL_ROOT=%RTM_ROOT%\coil set OMNI_ROOT=%RTM_ROOT%\omniORB if not DEFINED ARCH set ARCH=x86 if not DEFINED VC_VERSION set VC_VERSION=12 if not DEFINED PYTHON_DIR set PYTHON_DIR=c:\python27 if not DEFINED RTM_VERSION set RTM_VERSION=1.1.2 if not DEFINED OMNI_VERSION set OMNI_VERSION=4.1.7 if not DEFINED OMNITHREAD_VERSION set OMNITHREAD_VERSION=3.4
Edit the following libRTC_vc12.vcxproj, libcoil_vc12.vcxproj, libRTCSkel_vc12.vcxproj project file. Please edit libRTC_vc11.vcxproj, libcoil_vc11.vcxproj, libRTCSkel_vc11.vcxproj etc. according to the version of Visual Studio.
First, change the runtime library. Change MultiThreadedDebugDLL to MultiThreadedDebug as shown below. Whether editing is different depending on whether it is 32 bit or 64 bit, there is no problem even if both are changed.
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
Likewise, change MultiThreadedDLL to MultiThreaded.
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
Then change it to generate a static library instead of a dynamic library. Since libRTCSkel_vc12.vcxproj originally generates a static library, there is no need to change it.
Please change DynamicLibrary to StaticLibrary.
<ConfigurationType>DynamicLibrary</ConfigurationType>
<ConfigurationType>StaticLibrary</ConfigurationType>
Also change the output file name accordingly.
<OutputFile>$(OutDir)\RTC$(rtm_dllver).dll</OutputFile>
<OutputFile>$(OutDir)\RTC$(rtm_dllver).lib</OutputFile>
<OutputFile>$(OutDir)\RTC$(rtm_dllver)d.dll</OutputFile>
<OutputFile>$(OutDir)\RTC$(rtm_dllver)d.lib</OutputFile>
<OutputFile>$(OutDir)\coil$(coil_dllver).dll</OutputFile>
<OutputFile>$(OutDir)\coil$(coil_dllver).dll</OutputFile>
<OutputFile>$(OutDir)\coil$(coil_dllver)d.dll</OutputFile>
<OutputFile>$(OutDir)\coil$(coil_dllver)d.dll</OutputFile>
Also change the command to copy the file.
<Command>copy "$(OutDir)\\$(TargetName).lib" "$(SolutionDir)bin\\RTC$(rtm_dllver)d.lib"
copy "$(OutDir)\\RTC$(rtm_dllver)d.dll" "$(SolutionDir)bin\\"
</Command>
<Command>copy "$(OutDir)\\$(TargetName).lib" "$(SolutionDir)bin\\RTC$(rtm_dllver)d.lib"
</Command>
<Command>copy "$(OutDir)\\$(TargetName).lib" "$(SolutionDir)bin\\RTC$(rtm_dllver).lib"
copy "$(OutDir)\\RTC$(rtm_dllver).dll" "$(SolutionDir)bin\\"
</Command>
<Command>copy "$(OutDir)\\$(TargetName).lib" "$(SolutionDir)bin\\RTC$(rtm_dllver).lib"
;</Command>
<Command>copy "$(OutDir)\\$(TargetName).lib" "$(SolutionDir)bin\\coil$(coil_dllver)d.lib"
copy "$(OutDir)\\coil$(coil_dllver)d.dll" "$(SolutionDir)bin\\"
</Command>
<Command>copy "$(OutDir)\\$(TargetName).lib" "$(SolutionDir)bin\\coil$(coil_dllver)d.lib"
</Command>
<Command>copy "$(OutDir)\\$(TargetName).lib" "$(SolutionDir)bin\\coil$(coil_dllver).lib"
copy "$(OutDir)\\coil$(coil_dllver).dll" "$(SolutionDir)bin\\"
</Command>
<Command>copy "$(OutDir)\\$(TargetName).lib" "$(SolutionDir)bin\\coil$(coil_dllver).lib"
</Command>
Because errors occur in OpenRTM-aist-1.1.2 and omniORB-4.1.7, please add RTM_OMNIORB_41 to PreprocessorDefinitions of libRTC_vc12.vcxproj. It is unnecessary in OpenRTM-aist-1.2.0 or omniORB-4.2.1.
<PreprocessorDefinitions>LIBRARY_EXPORTS;WIN32;_DEBUG;_WINDOWS;_USRDLL;__WIN32__;__NT__;__OSVERSION__=4;__x86__;_WIN32_WINNT=0x0400;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>LIBRARY_EXPORTS;WIN32;_DEBUG;_WINDOWS;_USRDLL;__WIN32__;__NT__;__OSVERSION__=4;__x86__;_WIN32_WINNT=0x0400;_CRT_SECURE_NO_DEPRECATE;RTM_OMNIORB_41;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Change omniORB's linked library. Please edit the following file called rtm_config.props.
C:\openrtm_work\OpenRTM-aist\rtm_config.props
Change the following parts.
<omni_lib>omniORB$(omni_dllver)_rt.lib;omniDynamic$(omni_dllver)_rt.lib;omnithread$(omnithread_dllver)_rt.lib</omni_lib> <omni_libd>omniORB$(omni_dllver)_rtd.lib;omniDynamic$(omni_dllver)_rtd.lib;omnithread$(omnithread_dllver)_rtd.lib</omni_libd>
<omni_lib>omniORB4.lib;omniDynamic4.lib;omnithread.lib</omni_lib> <omni_libd>omniORB4d.lib;omniDynamic4d.lib;omnithreadd.lib</omni_libd>
When you start build.bat, it starts the build. When finished, check that the following files are generated in the bin folder.
A lot of errors are generated by compiling sample components, rtcd, rtcprof, but ignore it as it is not necessary if you build your own RTC.
Because the library name to be linked by omniORB has changed and it is necessary to add a library to be newly linked, edit the following file.
set(OMNIORB_LIBRARIES optimized;omniORB417_rt; optimized;omniDynamic417_rt; optimized;omnithread34_rt; debug;omniORB417_rtd; debug;omniDynamic417_rtd; debug;omnithread34_rtd)
set(OMNIORB_LIBRARIES optimized;omniORB4; optimized;omniDynamic4; optimized;omnithread; debug;omniORB4d; debug;omniDynamic4d; debug;omnithreadd)
set(OPENRTM_LIBRARIES optimized;RTC112_vc12; optimized;coil112_vc12; optimized;omniORB4_rt; optimized;omniDynamic4_rt; optimized;omnithread4_rt; optimized;advapi32; optimized;ws2_32; optimized;mswsock; debug;RTC112_vc12d; debug;coil112_vc12d; debug;omniORB4_rtd; debug;omniDynamic4_rtd; debug;omnithread4_rtd; debug;advapi32; debug;ws2_32; debug;mswsock)
set(OPENRTM_LIBRARIES optimized;RTC112_vc12; optimized;coil112_vc12; optimized;omniORB4; optimized;omniDynamic4; optimized;omnithread; optimized;advapi32; optimized;ws2_32; optimized;mswsock; optimized;libRTCSkel; optimized;rpcrt4; debug;RTC112_vc12d; debug;coil112_vc12d; debug;omniORB4d; debug;omniDynamic4d; debug;omnithreadd; debug;advapi32; debug;ws2_32; debug;mswsock; debug;libRTCSkeld; debug;rpcrt4)
Please note that this file will be overwritten if you build OpenRTM - aist again.
Next, set /MT, /MTd and edit the RTC file you want to build. Edit CMakeLists.txt in the src folder. By adding the following description, set the runtime library to /MT, /MTd.
set(CMAKE_CXX_FLAGS_RELEASE "/MT") set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
Next, set the OpenRTM-aist and omniORB to be used for the build to the one that was built previously and build RTC. From the command prompt, enter the following command to set the variable.
set RTM_ROOT=C:\openrtm_work\OpenRTM-aist set VC_VERSION=12 set OMNI_ROOT=C:\openrtm_work\OpenRTM-aist\omniORB set OpenRTM_DIR=C:\openrtm_work\OpenRTM-aist\cmake
Please change accordingly depending on the version of Visual Studio etc. Finally it is done by building with the following command.
cmake . cmake --build . --config Debug
If the /MT executable file is linked with some sort of DLL, the following error message may be displayed.
Expression: _pFirstBlock == pHead
Or releasing the memory allocated on the executable file side on the DLL side.
__declspec(dllexport) void deleteData(char* v) { delete v; }
void deleteData(char* v); int main(void) { char *v = new char[1000]; deleteData(v); }
This seems that when /MT, /MTd is specified in the runtime library, heap destruction may occur because the heap area is created separately in the executable file and the DLL. However, it does not occur reliably, for example if you put a print statement before executing the deleteData function, it will not happen why. In this case, since it falls at a place not related at all afterwards, it becomes very difficult to identify the cause.
On the other hand, if /MD, /MDd is specified in the runtime library, this problem does not occur because the heap area is shared.
Basically, you should use /MD, /MDd for the runtime library because it is impossible to check and fix where abnormality occurs in OpenRTM-aist or omniORB.
If you really want to set /MT, /MTd in the runtime library, create a static library using the above method.
Although RTC specified by /MT, /MTd can be operated by the above method, omniORB and coil are linked to a library named advapi32.dll, and advapi32.dll is /MD's runtime library ( MSVCRT), we do not know what will happen here.