如何使用 mexsh 将 link 非 .o 目标文件转换为 mex 可执行文件?

How to use mexsh to link non-.o object files into a mex executable?

我在 Ubuntu 14.04 和 GCC 4.8.4 上使用 MATLAB 2015b。

我正在尝试在一个 Makefile 中管理 C 和 C++ 代码。

C 和 C++ 代码需要使用不同的编译器标志。为了解决这个问题,我尝试将 .cpp 文件编译为 .xo 目标文件,将 .c 文件编译为 .o 目标文件,这样我就可以分别对 .xo 和 .o 使用不同的配方。

这是我的 Makefile 示例:

CC=gcc
CXX=g++
MEX=/usr/local/MATLAB/R2015b/bin/mexsh

CFLAGS=-Wall -fPIC -std=gnu99
CXXFLAGS=-Wall -fPIC -std=gnu++11

INCLUDES=/usr/include/boost

LFLAGS=-lm -L/usr/lib -lboost_system -lboost_regex
all: hello asio

hello: hello.o               # hello world example, C code, works fine
    $(CC) -o $@ $^ $(LFLAGS)
asio:asio.xo                 # boost asio example, C++ code, works file
    $(CXX) -o $@ $^ $(LFLAGS)
mexAsio:asio.xo
    $(MEX) $^ $(LFLAGS) ############  here, with .xo files as input
                        ############  mex gives "no input file" error
clean:
    rm -f *.o
    rm -f *.xo

%.o: %.c 
    $(CC) -c -o $@ $< $(CFLAGS) -I$(INCLUDES)
%.xo: %.cpp 
    $(CXX) -c -o $@ $< $(CXXFLAGS) -I$(INCLUDES)

GCC 和 G++ 没有问题 linking 具有任意扩展名的目标文件,但是当我尝试使用 mex 来 link 目标文件来创建 mex 可执行文件时,mex 命令只接受 .o文件。

问题:

1) 有没有办法让 mex 接受具有任意扩展名的目标文件?

2) 是否有另一种方法可以使用不同的编译器标志编译 C++ 和 C 代码,而不是对目标文件目标使用自定义扩展?

1) Is there a way to make mex accept object files with arbitrary extension?

不,mex 是其他编译器的包装脚本,不接受目标文件的任意扩展名。

2) Is there another way to compile C++ and C code with different compiler flags rather than using custom extension for object file targets?

您已经这样做了

%.o: %.c 
    $(CC) -c -o $@ $< $(CFLAGS) -I$(INCLUDES)
%.xo: %.cpp 
    $(CXX) -c -o $@ $< $(CXXFLAGS) -I$(INCLUDES)

我只想更改您的 Makefile 中的以下内容:

  • %.xo: %.cpp -> %.o: %.cpp
  • asio:asio.xo -> asio:asio.o
  • mexAsio:asio.xo -> mexAsio:asio.o

您可能需要将 $(MEX) $^ $(LFLAGS) 更改为 $(MEX) -cxx $^ $(LFLAGS)