compile .C file Include library using mex - error LNK2019: u

Post Reply
orhirshfeld
Posts: 16
Joined: Tue Jun 17, 2014 9:42 am

compile .C file Include library using mex - error LNK2019: u

Post by orhirshfeld »

Hello I'm trying to build simple C function with external commands provide by library NPTrackingTools API

Code: Select all

 #include <stdio.h>
 #include "mex.h"
 #include "NPTrackingTools.h"
 void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
    int ret1,ret2,ret3;
    ret1=TT_Initialize();
    ret2=TT_FinalCleanup();
    ret3=TT_Shutdown();
    printf("Hello, World! \n");
    return 0;
}
I tried the following compiler code

Code: Select all

mex -v '-Id:\or hirshfeld\onedrive\work control lab aero summer 2014 technio\C_code' '-LD:\or hirshfeld\onedrive\work control lab aero summer 2014 technion\C_code' '-lNPTrackingToolsx64.lib' test_C_compile_with_include_trackingtools.c
I have all files in the same directory "d:\or hirshfeld\onedrive\work control lab aero summer 2014 technio\C_code"

but the compiler return an error that he can't understand my commands
Error using mex
Creating library test_C_compile_with_include_trackingtools.lib and object test_C_compile_with_include_trackingtools.exp
test_C_compile_with_include_trackingtools.obj : error LNK2019: unresolved external symbol __imp_TT_Initialize referenced in
function mexFunction
test_C_compile_with_include_trackingtools.obj : error LNK2019: unresolved external symbol __imp_TT_Shutdown referenced in function
mexFunction
test_C_compile_with_include_trackingtools.obj : error LNK2019: unresolved external symbol __imp_TT_FinalCleanup referenced in
function mexFunction
test_C_compile_with_include_trackingtools.mexw64 : fatal error LNK1120: 3 unresolved externals
I tried different variation but still have the same problem, can you help me?

github project
https://github.com/orhirshfeld/Motive-S-function

Thanks

Or Hirshfeld

אור הירשפלד
steven.andrews
NaturalPoint Employee
NaturalPoint Employee
Posts: 720
Joined: Mon Jan 19, 2015 11:52 am

Re: compile .C file Include library using mex - error LNK201

Post by steven.andrews »

Hello Or,

When compiling a library that uses the NPTrackingTools.h header file, you will also need to link NPTrackingTools.LIB.
If you do not link NPTrackingTools.LIB, you will get undefined symbols. This appears to be the issue you are running into here.

I hope this information helps you to compile your project. If you require any further assistance, please feel free to reach out to us through your open support ticket.

Best regards,
Steven
--
Steven Andrews
OptiTrack | Customer Support Engineer
orhirshfeld
Posts: 16
Joined: Tue Jun 17, 2014 9:42 am

Re: compile .C file Include library using mex - error LNK201

Post by orhirshfeld »

Steven, thank you for the response

I succed solving the problem by renaming the file extnsion to CPP so it compile C++

First i tried to run it with NPTrackingTools.LIB using the following compiler code:

Code: Select all

mex -v test_C_compile_with_include_trackingtools.cpp -I"D:\or hirshfeld\onedrive\work control lab aero summer 2014 technion\C_code\Motive-S-function" '-LD:\or hirshfeld\onedrive\work control lab aero summer 2014 technion\C_code\Motive-S-function' '-lNPTrackingTools'
Error using mex
NPTrackingTools.lib(NPTrackingTools.dll) : fatal error LNK1112: module machine type 'X86'
conflicts with target machine type 'x64'


So i just switched the compiler code to link to the 64bit version NPTrackingToolsx64.LIB and also made the compiler line shorter:

Code: Select all

mex -v test_C_compile_with_include_trackingtools.cpp '-lNPTrackingToolsx64'
And it worked

Thanks to chappjc (http://stackoverflow.com/users/2778484/chappjc) from Stack Overflow forum (http://stackoverflow.com/questions/2988 ... 2#29881292 )

Or Hirshfeld
אור הירשפלד
steven.andrews
NaturalPoint Employee
NaturalPoint Employee
Posts: 720
Joined: Mon Jan 19, 2015 11:52 am

Re: compile .C file Include library using mex - error LNK201

Post by steven.andrews »

Hello Or,

It looks like you've gotten your code to compile successfully. I'm glad your project is now working.
Best of luck with your work.

Cheers,
Steven
--
Steven Andrews
OptiTrack | Customer Support Engineer
Post Reply