[vtkusers] Building a VTK-leveraging DLL and driver EXE using CMake -- Need help!

Chris Volpe ARA/SED cvolpe at ara.com
Mon Mar 21 18:51:03 EDT 2011


I'm following-up to my own post here because I just discovered in the latest version of the CMake User's Guide, page 25, that this behavior is by design. The example given is:

add_library(foo foo.cxx)
target_link_libraries(foo bar)

add_executable(foobar foobar.cxx)
target_link_libraries(foobar foo)

The text below the example states, "This will link the libraries foo and bar into the executable foobar even [sic], although only foo was explicitly linked into foobar. With shared or DLL builds this linking is not always needed, but the extra linkage is harmless."

I believe this behavior is clearly not harmless, as the problem I described below makes clear. Is there a workaround?

Thanks,

-Chris

From: vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] On Behalf Of Chris Volpe ARA/SED
Sent: Monday, March 21, 2011 3:59 PM
To: David Partyka
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Building a VTK-leveraging DLL and driver EXE using CMake -- Need help!

Thank you, David. I figured I'd have to do that anyway to see the symbols, but I didn't realize that failing to do so would preclude generation of even an empty .lib file. I thought the missing .lib was a separate problem.

After  fixing the problem as you describe below, one of my issues goes away: If I manually remove the reference to vtkGraphics.lib (and Hybrid and Rendering) from the TestDriver linker inputs, I no longer get a bunch of unresolved symbols - they're now resolved by FeatureViewer. But  I still have the issue that the TestDriver executable won't link without the above hack because it can't find vtkGraphics.lib. Note that TestDriver itself does not reference any vtk symbols, only the FeatureViewer library does. So why is CMake creating a TestDriver project that demands vtkGraphics.lib? Any thoughts?

Thanks again,
-Chris

From: vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] On Behalf Of David Partyka
Sent: Friday, March 18, 2011 8:47 PM
To: Chris Volpe ARA/SED
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Building a VTK-leveraging DLL and driver EXE using CMake -- Need help!

Your dll has no lib because you are not exporting any symbols. You'll want to make a common header for your classes that has a block similar to

http://stackoverflow.com/questions/4276549/exporting-symbols-when-compiling-dll-msvc

And then in each of your class declarations use your libraries export macro.

For example classes in vtkCommon are exported like so:

class VTK_COMMON_EXPORT vtkArray : public vtkObject

And VTK_COMMON_EXPORT is defined by this block in vtkWin32Header.h

 #if defined(vtkCommon_EXPORTS)
  #define VTK_COMMON_EXPORT VTK_ABI_EXPORT
 #else
  #define VTK_COMMON_EXPORT VTK_ABI_IMPORT
 #endif


And VTK_ABI_IMPORT/VTK_ABI_EXPORT are defined in vtkABI.h

#if defined(_WIN32)
# define VTK_ABI_IMPORT __declspec(dllimport)
# define VTK_ABI_EXPORT __declspec(dllexport)
# define VTK_ABI_HIDDEN
#elif __GNUC__ >= 4
# define VTK_ABI_IMPORT __attribute__ ((visibility("default")))
# define VTK_ABI_EXPORT __attribute__ ((visibility("default")))
# define VTK_ABI_HIDDEN __attribute__ ((visibility("hidden")))
#else
# define VTK_ABI_IMPORT
# define VTK_ABI_EXPORT
# define VTK_ABI_HIDDEN
#endif

On Fri, Mar 18, 2011 at 8:28 PM, Chris Volpe ARA/SED <cvolpe at ara.com<mailto:cvolpe at ara.com>> wrote:
Hello-

I am trying to set up a source tree which will allow CMake to create a MSVC++ .sln file that contains the following two projects:

1.       A DLL (called "FeatureViewer") containing a vanilla C++ class that links against several VTK kits (Graphics, Rendering, and Hybrid).

2.       An EXE (called "TestDriver") that links against the aforementioned library containing the vanilla C++ class.

I have built VTK 5.6 from source, and I want to deliver the DLL, LIB, and class header file from (1) above to a co-worker who has his own application and wants to use the functionality I'm encapsulating, but he doesn't want to "vtk-ify" his build process. The EXE in (2) above is simply my test driver for (1).

My problem is that the EXE won't build because the generated project is spuriously looking for vtk libraries (e.g. vtkGraphics.lib et. al.) at link time that it doesn't directly reference, and it doesn't know where to find them. The EXE shouldn't need to know about them because their use is strictly within the FeatureViewer library. If I go into the EXE project properties and manually delete the references to vtkGraphics.lib et. al. from the linker->input->additional-dependencies list, I get a whole bunch of unresolved symbols for the stuff in the vtk libs. I figured out that part of my problem is that the FeatureViewer library was being built static (there was no dll), so I changed the CMakeLists.txt file for the library so that it builds SHARED. Now, I get a DLL, but there's now no .lib for my EXE to link against.

Can someone tell me what I'm doing wrong? Here's what I'm doing in the three CMakeLists.txt files (top level, library subdir, executable subdir)

Top Level CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.4)
IF(COMMAND CMAKE_POLICY)
  CMAKE_POLICY(SET CMP0003 NEW)
ENDIF(COMMAND CMAKE_POLICY)
PROJECT(FeatureViewer)
SUBDIRS (
  FeatureViewer
  TestDriver
)
#INCLUDE_DIRECTORIES(${FeatureViewer_SOURCE_DIR}/FeatureViewer)

Library CMakeLists.txt file in FeatureViewer subdir:

SET (FeatureViewer_SRCS
  FeatureViewer.cxx
)
IF(NOT VTK_BINARY_DIR)
FIND_PACKAGE(VTK REQUIRED)
IF(NOT VTK_USE_RENDERING)
  MESSAGE(FATAL_ERROR "Example ${PROJECT_NAME} requires VTK_USE_RENDERING.")
ENDIF(NOT VTK_USE_RENDERING)
INCLUDE(${VTK_USE_FILE})
ENDIF(NOT VTK_BINARY_DIR)
ADD_LIBRARY(FeatureViewer SHARED ${FeatureViewer_SRCS})
TARGET_LINK_LIBRARIES(FeatureViewer vtkGraphics vtkRendering vtkHybrid)

Executable CMakeLists.txt file in TestDriver subdir:

ADD_EXECUTABLE(TestDriver TestDriver.cxx)
TARGET_LINK_LIBRARIES(TestDriver FeatureViewer)
INCLUDE_DIRECTORIES(${FeatureViewer_SOURCE_DIR}/FeatureViewer)

When I try to build, FeatureViewer builds ok (DLL is there, but no LIB in sight), but TestDriver fails with the following:
2>LINK : fatal error LNK1104: cannot open file '..\FeatureViewer\Debug\FeatureViewer.lib'

This seems straightforward enough. Can anyone tell me what I'm doing wrong? Please let me know if the cmake mailing list is the more appropriate venue for this question, and I will re-post there. Thanks so much in advance for any assistance you can provide.

Chris
--
Christopher R. Volpe, Ph.D.                                                           Email: cvolpe at ara.com<mailto:cvolpe at ara.com>
Senior Scientist, Information Exploitation Systems             Main Desk: 919-582-3300
Applied Research Associates, Inc<http://www.ara.com/>                                                     Direct: 919-582-3380
8537 Six Forks Rd., Suite 6000                                                            Fax : 919-582-3301
Raleigh, NC 27615                                         Web: http://www.ara.com/offices/NC.htm



_______________________________________________
Powered by www.kitware.com<http://www.kitware.com>

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110321/3d1b7e32/attachment.htm>


More information about the vtkusers mailing list