问题
Hey so I'm working to attach a piece of hardware into a setup controlled with MATLAB. I've written a set of functions (tried both C and C++) that utilize this hardware to perform simple tasks.
The code compiles just fine using cl. When I try to compile with mex however, it cannot seem to link symbols from my includes (am using the -I flag pointing to directory of my header files).
Any thoughts?
CL Output:
>cl isConnected.c *.lib
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29334 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
isConnected.c
Microsoft (R) Incremental Linker Version 14.28.29334.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:isConnected.exe
isConnected.obj
ArenaCd_v140.lib
ArenaC_v140.lib
isConnectedMex.lib
SaveCd_v140.lib
SaveC_v140.lib
MATLAB Output:
>> curDir = pwd;
>> linkPath = ['-L' fullfile(curDir,'\lib64\ArenaC\ArenaC_v140.lib')];
>> incPath = ['-I' fullfile(curDir,'\include\ArenaC')];
>> exist(linkPath(3:end))
ans = 2
>> exist(incPath(3:end))
ans = 7
>> % Paths Are Correctly named it seems
>> mex('isConnectedMex.c',incPath,linkPath)
Building with 'Microsoft Visual C++ 2019 (C)'.
Error using mex
Creating library isConnectedMex.lib and object isConnectedMex.exp
isConnectedMex.obj : error LNK2019: unresolved external symbol __imp_acOpenSystem referenced in function main
isConnectedMex.obj : error LNK2019: unresolved external symbol __imp_acGetLastErrorMessage referenced in function main
isConnectedMex.obj : error LNK2019: unresolved external symbol __imp_acSystemUpdateDevices referenced in function main
isConnectedMex.obj : error LNK2019: unresolved external symbol __imp_acSystemGetNumDevices referenced in function main
isConnectedMex.mexw64 : fatal error LNK1120: 4 unresolved externals
The functions referenced by MATLAB errors are #include in one of my headers. Using -v option with MEX though it shows these folders are being included. Any help would be greatly appreciated!!
Edit: Adding cl and mex commands used. The only difference between isConnected.c and isConnectedMex.c is that isConnectedMex.c uses the mexFunction call as it's main method.
回答1:
The -L
option is to specify a directory where the linker can find required library files. You need to use the -l
option (lowercase L) to specify which libraries to link. This is what it looks like:
mex isConnectedMex.c -Iinclude\ArenaC -Llib64\ArenaC -lArenaC_v140
Or, if the lib file is a static library, you can pass it as an input file:
mex isConnectedMex.c lib64\ArenaC\ArenaC_v140.lib -Iinclude\ArenaC
来源:https://stackoverflow.com/questions/65424607/why-does-my-code-fail-to-buld-with-mex-but-work-with-cl