Open3D icon indicating copy to clipboard operation
Open3D copied to clipboard

Adding separate staticlib folder

Open Jason420Git opened this issue 8 years ago • 9 comments

I am running 2 different projects using Open3D. One of them links Open3D statically, the other dynamically. Both are on Windows.

The switch to create static project or dynamic is very helpful for me. However I still need to recompile / install when switching between static and dynamic. Would it be possible to create a 'staticlib' folder that is found by find_package if the project is setup to be static? Then the dynamic is in the lib folder.

Jason420Git avatar Nov 15 '18 15:11 Jason420Git

To clarify, when you say static v.s. dynamic, which one do you mean?

  1. Whether we build Open3D as a dynamic (open3d.dll) or a static (open3d.lib) library.
  2. Whether we link with Windows static runtime (/MT or /MTd) or dynamic runtime (/MD or /MDd)

yxlao avatar Nov 15 '18 20:11 yxlao

  1. here. I am able to toggle on and off the Cmake switch for building windows static runtime. But when I switch to my other project which is using dynamic runtime, I have to rebuild open3D since it always overwrites the lib folder. I sometimes save both and rename the folders before running my other project but since Open3D is changing rapidly it becomes quite time consuming.

Jason420Git avatar Nov 16 '18 16:11 Jason420Git

Yeah, that seems like a very specific use case. If we want to achieve that, it seems like we have to config twice and build twice -- though the second build shall be faster, since most compiled object files can be reused, it's just the linking option that's different.

Theoretically, it should be possible to implement that in CMake by introducing two sets of build targets. However, it seems to add too much complexity. Rather than baking it into the Open3D cmake, would it be possible to create a .bat script to achieve this?

cmake -DSOME_FLAGS  -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=ON ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Release --target ALL_BUILD
xcopy lib lib_with_static_runtime /s /e /h

cmake -DSOME_FLAGS  -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=OFF ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Release --target ALL_BUILD
xcopy lib lib_with_dynamic_runtime /s /e /h

yxlao avatar Nov 18 '18 06:11 yxlao

Hi, yes that may be an option for me to create a script to pass to my team. I think we would want to take it further and have Debug builds as well since we also use Debug mode. Would this approach still work with Open3D's find_package?

Jason420Git avatar Nov 26 '18 13:11 Jason420Git

@yxlao: do you think we need to support this use case? Could you follow-up?

syncle avatar Dec 17 '18 18:12 syncle

This feature is essentially: (1) config in multiple ways -> (2) run build command once -> (3) produce multiple targets in separate folders.

Though it may be possible to do it in CMake, but it's more natural to do it outside of the CMake system by having a custom script, since CMake is typically designed for "one config -> one build" use case. Adding a generic solution to support multiple configs at the same time (e.g. linking static & dynanmic; Debug & Release for this one) would create a lot of complexity in CMake. So my vote is to handle it outside of the CMake system.

For @Jason420Git 's question, yes, this approach will still work for find_package, as long as we we specify the corresponding paths to the build correctly.

yxlao avatar Dec 17 '18 19:12 yxlao

I have created the following script, as per your recommendation:

mkdir build
mkdir install
cd build

cmake -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=ON ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Release --target ALL_BUILD
xcopy lib ..\install\lib_static\ /s /e /h /y

cmake -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=OFF ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Release --target ALL_BUILD
xcopy lib ..\install\lib_dynamic\ /s /e /h /y

cmake -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=ON ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Debug --target ALL_BUILD
xcopy lib ..\install\lib_static\ /s /e /h /y

cmake -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=OFF ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Debug --target ALL_BUILD
xcopy lib ..\install\lib_dynamic\ /s /e /h /y`

This will create static,dynamic and debug,release versions of both. So I end up with lib_static and lib_dynamic, which have Debug and Release underneath.

I am uncertain how to get find package to select the correct library from this point onwards. I can supply a hint, but that is expecting the Cmake directory. e.g. we have something like this: find_package(Open3D REQUIRED HINTS ${CMAKE_INSTALL_PREFIX}/lib/CMake

Would it not require changes to the find package script in order to get the desired functionality? I would prefer not to modify any existing Open3D code.

Jason420Git avatar Jan 14 '19 14:01 Jason420Git

I have the following solution working for me. It is not ideal and it would be easier to make the changes in the find open3d file but posting in case it is useful to anyone else.

In run the following .bat file from inside open3d root directory:

mkdir install
cd build

cmake -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=ON ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Release --target ALL_BUILD
xcopy lib ..\install\lib_static\ /s /e /h /y

cmake -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=OFF ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Release --target ALL_BUILD
xcopy lib ..\install\lib_dynamic\ /s /e /h /y

cmake -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=ON ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Debug --target ALL_BUILD
xcopy lib ..\install\lib_static\ /s /e /h /y

cmake -G "Visual Studio 14 2015 Win64" -DSTATIC_WINDOWS_RUNTIME=OFF ..
cmake --build . --parallel %NUMBER_OF_PROCESSORS% --config Debug --target ALL_BUILD
xcopy lib ..\install\lib_dynamic\ /s /e /h /y```

Then in my project I setup my config with the following lines. Note: MSVC auto adds /Debug or /Release depending on which configuration you are compiling in, to your link_directories path.

```if(USE_OPEN3D)
	find_package(Open3D REQUIRED)
	if(Open3D_FOUND)
		message(STATUS "Found Open3D ${Open3D_VERSION}")
		include_directories(${Open3D_INCLUDE_DIRS})
		
		#Compiling in static
		if(USE_OPEN3D_WINDOWS_BUILD_SCRIPT)
			link_directories("${Open3D_LIBRARY_DIRS}/static")
		else()
			link_directories(${Open3D_LIBRARY_DIRS})
		endif()```


Jason420Git avatar Jan 22 '19 09:01 Jason420Git

@Jason420Git

Hi,

I would like to build dynamic library. I am keeping two separate folders for dynamic and static libraries. That works for me. I would like to ask how you build dynamic library through CMake?

Is it enough to set the flag like this? Or Do I need to do smth else?

aa

petrasvestartas avatar Dec 09 '20 13:12 petrasvestartas