Skip to content

Commit

Permalink
fix linking exe against matlab libs on ubuntu (once again the compile…
Browse files Browse the repository at this point in the history
…r here was overly sensitive about the order of the arguments)
  • Loading branch information
RussTedrake committed Jan 31, 2014
1 parent 08b295d commit b32a657
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions cmake/mex.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ function(mex_setup)
endfunction()

function(add_mex)
# useage: add_mex(target source1 source2 [OBJECT])
# if OBJECT is passed in, then it doesn't expect a mexFunction symbol to be defined, and compiles it to e.g., libtarget.so, for eventual linking against another mex file
# useage: add_mex(target source1 source2 [SHARED,EXECUTABLE])
# note: builds the mex file inplace (not into some build directory)
# if SHARED is passed in, then it doesn't expect a mexFunction symbol to be defined, and compiles it to e.g., libtarget.so, for eventual linking against another mex file
# if EXECUTABLE is passed in, then it adds an executable target, which is linked against the appropriate matlab libraries.

list(GET ARGV 0 target)
list(REMOVE_AT ARGV 0)
Expand Down Expand Up @@ -145,7 +146,22 @@ function(add_mex)
COMPILE_FLAGS "-DMATLAB_MEX_FILE"
)

if (isshared EQUAL -1 AND isexe EQUAL -1)
if (isexe GREATER -1)
# see note below
if (NOT TARGET last)
set(dummy_c_file ${CMAKE_CURRENT_BINARY_DIR}/dummy.c)
add_custom_command(COMMAND ${CMAKE_COMMAND} -E touch ${dummy_c_file}
OUTPUT ${dummy_c_file})
add_library(last STATIC ${dummy_c_file})
target_link_libraries(last ${MEX_CLIBS})
endif()

target_link_libraries(${target} last)
elseif (isshared GREATER -1)
set_target_properties(${target} PROPERTIES
LINK_FLAGS "${MEX_CLIBS}"
)
else()
# note: on ubuntu, gcc did not like the MEX_CLIBS coming along with LINK_FLAGS (it only works if they appear after the input files). this is a nasty trick that I found online
if (NOT TARGET last)
set(dummy_c_file ${CMAKE_CURRENT_BINARY_DIR}/dummy.c)
Expand All @@ -165,10 +181,6 @@ function(add_mex)
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
target_link_libraries(${target} last)
else()
set_target_properties(${target} PROPERTIES
LINK_FLAGS "${MEX_CLIBS}"
)
endif()

# todo: add CLIBS or CXXLIBS to LINK_FLAGS selectively based in if it's a c or cxx target (always added CXX above)
Expand Down

0 comments on commit b32a657

Please sign in to comment.