{"id":10376,"date":"2021-03-26T16:08:22","date_gmt":"2021-03-26T20:08:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=10376"},"modified":"2021-04-07T09:23:15","modified_gmt":"2021-04-07T13:23:15","slug":"linking-c-mex-s-function-to-c-code","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2021\/03\/26\/linking-c-mex-s-function-to-c-code\/","title":{"rendered":"Linking C MEX S-Function to C++ Code"},"content":{"rendered":"<div class = rtcContent><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span style=' font-style: italic;'>Today, I am happy to welcome guest blogger <\/span><a href = \"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/15996071\"><span>Navid Rahbariasr<\/span><\/a><span style=' font-style: italic;'>. Navid recently received a request from a user trying to include C++ code in a referenced model in accelerator mode. Unfortunately, this user was forced to use MATLAB R2020b and was not able to take advantage of the new <\/span><a href = \"https:\/\/www.mathworks.com\/help\/simulink\/release-notes.html#mw_6057f456-5386-4a5c-bd62-f69f733891fa\"><span style=' font-style: italic;'>C++ support for model reference simulation target<\/span><\/a><span style=' font-style: italic;'> added in MATLAB R2021a. Here is the workaround Navid came up with.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span style=' font-style: italic;'>--Guy<\/span><\/div><h3  style = 'margin: 15px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span>Configuring the S-Function Builder to include C++ Code<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Let's start with a simple example where our S-Function is calling a method <\/span><span style=' font-style: italic;'>get_a<\/span><span> defined in custom C++ source code. The header and the source code for the custom C++ code are as follows:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_1.png\" width = \"350\" height = \"259\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_2.png\" width = \"327\" height = \"247\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Note that \"::\" is a C++ operator. It is called the <\/span><a href = \"https:\/\/en.wikipedia.org\/wiki\/Scope_resolution_operator\"><span>scope resolution operator<\/span><\/a><span> and can be used to access a global variable when there is a local variable with the same name. So, in this example we have a global variable named <\/span><span style=' font-style: italic;'>a<\/span><span>, and a local variable with the same name inside the method. By adding :: in front of <\/span><span style=' font-style: italic;'>a<\/span><span> we indicate our interest in the global variable <\/span><span style=' font-style: italic;'>a<\/span><span>, which in this example has the value of 2. This operator :: is not defined in C, so if you try to compile this code in C, you will get a syntax error.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Now we use the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2021a\/simulink\/slref\/sfunction.html\"><span>S-Function Builder<\/span><\/a><span> to create an S-function that calls the <\/span><span style=' font-style: italic;'>get_a <\/span><span>function and routes the value to output. In case you did not notice, the S-Function Builder has been entirely revamped in R2020b, allowing you to see the final S-Function code interactively.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>We include the custom header in the includes section of the S-Function Builder as shown below:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_3.png\" width = \"502\" height = \"505\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>We call the method <\/span><span style=' font-style: italic;'>get_a<\/span><span> in the S-function outputs wrapper function and assign it to the output of the S-Function block:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_4.png\" width = \"500\" height = \"337\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>We add the C++ source code as a library entry so that S-Function can call the method defined in it. You can find the library entry at the bottom of the S-function Builder interface:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_5.png\" width = \"614\" height = \"278\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>And finally, we build the S-Function: <\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_6.png\" width = \"528\" height = \"177\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Note that we have set the Language of the S-Function to C++. If we set it to C, we will get an error that method <\/span><span style=' font-style: italic;'>get_a<\/span><span> is unresolved:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_7.png\" width = \"671\" height = \"120\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>By setting the language to C++, the S-function is built. Now  if we run the model and check the output, we see the output is 2, the value of the global variable <\/span><span style=' font-style: italic;'>a<\/span><span>:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_8.png\" width = \"510\" height = \"223\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><h3  style = 'margin: 15px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span>Referencing the Model<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>If we reference the above model in normal mode, we will get the expected value of 2.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_9.png\" width = \"557\" height = \"250\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>However, if we set the model block into accelerator mode and try to run the model, we see an error about an unresolved external symbol that looks quite similar to the one shown above when the S-Function Builder language was set to C instead of C++:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_10.png\" width = \"697\" height = \"264\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>The error suggests that Simulink cannot find the <\/span><span style=' font-style: italic;'>sfcn_test_wrapper.c<\/span><span> and <\/span><span style=' font-style: italic;'>custom_code.c -- <\/span><span>which is accurate because these files have cpp extension rather than c! This is happening because the Simulink engine is first generating a simulation target for the reference model in accelerator mode whose language is C, and thus it is interpreting the symbols as C rather than C++, therefore the accelerator mode build cannot correctly link with the S-Function.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_11.png\" width = \"486\" height = \"252\" alt = \"graphical_issue.png\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>This was a limitation of the simulation target in Simulink until R2021a, when <\/span><a href = \"https:\/\/www.mathworks.com\/help\/simulink\/release-notes.html#mw_6057f456-5386-4a5c-bd62-f69f733891fa\"><span style=' font-style: italic;'>C++ support for model reference simulation target<\/span><\/a><span style=' font-style: italic;'> <\/span><span>has been added.<\/span><\/div><h3  style = 'margin: 15px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span>Workaround<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>If we want the accelerated referenced model to build successfully, we need to write the S-Functions in C, but our source code is using C++ features, so how we can write the S-Function in C? <\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>The trick is to link the S-function to the compiled version of the source code, so the compiler does not need to recompile it (it is like grinding the C++ food for the C compiler that does not have the right teeth to chew it!) But can C-code call compiled C++ methods? The answer is yes, provided we wrap the C++ declaration methods with <\/span><span style=' font-style: italic;'>extern C {}<\/span><span>, so that the compiler uses C naming conventions when compiling the methods.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>We first modify our custom header file as below by wrapping the method with extern C:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_12.png\" width = \"409\" height = \"362\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Note that <\/span><span style=' font-style: italic;'>#ifdef __cplusplus<\/span><span> is a compiler directive. It effectively means \"if the language is C++...\". When we compile the source code, as the compiler detects it is C++ it wraps the method declaration with <\/span><span style=' font-style: italic;'>extern \"C\" { }<\/span><span> so the compiled object will have C naming schemes. <\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Now we compile the source code into an object file by using mex with -c flag:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span style=' font-style: italic;'>mex -c custom_source.cpp<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>By running this command, you will get an object file <\/span><span style=' font-style: italic;'>custom_code.obj <\/span><span>in the folder which contains the compiled version of the <\/span><span style=' font-style: italic;'>custom_source.cpp<\/span><span>:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_13.png\" width = \"235\" height = \"222\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Now we go to the S-Function builder and modify two things:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>1) Change the language to C<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>2) Link the S-function to the compiled obj file rather than the uncompiled source file (Surprise! You can link the S-Function to obj files!)<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_14.png\" width = \"499\" height = \"495\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>As we are linking the S-Function to a compiled C++ code rather than uncompiled C++ code, the S-Function can be built using the C language.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Now if we go to the top model and run the model in accelerated mode, it will build the simulation target successfully and we will get the expected output of 2:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_15.png\" width = \"515\" height = \"236\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Basically, we were able to link the accelerator build which only understands C language to the C MEX S-function and the compiled C++ obj file:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_16.png\" width = \"491\" height = \"266\" alt = \"graphical_wrkarnd.png\" style = \"vertical-align: baseline\"><\/img><\/div><h3  style = 'margin: 15px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span>C++ support for model reference simulation target in R2021a<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>The above workaround is useful for older releases. In R2021a, if your model includes C++ S-Functions, you can simply set the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2021a\/simulink\/gui\/specify-custom-code-language-setting.html\"><span>Language<\/span><\/a><span> to C++ in the Simulation Target section of the configuration set, which will allow the accelerator build to understand the C++ language and interpret the C++ S-Function correctly.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_17.png\" width = \"672\" height = \"296\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>and everything is built using C++:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_18.png\" width = \"463\" height = \"241\" alt = \"\" style = \"vertical-align: baseline\"><\/img><\/div><h3  style = 'margin: 15px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span>Now it's your turn<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Let us know in the comments below  if the addition of <\/span><a href = \"https:\/\/www.mathworks.com\/help\/simulink\/release-notes.html#mw_6057f456-5386-4a5c-bd62-f69f733891fa\"><span>C++ support for model reference simulation target<\/span><\/a><span> added in MATLAB R2021a opens new possibilities for you.<\/span><\/div>\r\n<\/div><script type=\"text\/javascript\">var css = ''; var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style'); head.appendChild(style); style.type = 'text\/css'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); }<\/script><a href=\"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2.mlx\"><button class=\"btn btn-sm btn_color_blue pull-right add_margin_10\">Download Live Script<\/button><\/a>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/blog_post_v2_18.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>Today, I am happy to welcome guest blogger Navid Rahbariasr. Navid recently received a request from a user trying to include C++ code in a referenced model in accelerator mode. Unfortunately, this... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2021\/03\/26\/linking-c-mex-s-function-to-c-code\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":10555,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,65,24,87,16],"tags":[613],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/10376"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=10376"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/10376\/revisions"}],"predecessor-version":[{"id":10575,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/10376\/revisions\/10575"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media\/10555"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=10376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=10376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=10376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}