However, I primarily use VS Code, and I’m wondering — can I use VS Code instead of Visual Studio Community for building and linking DLLs in PLECS?
Will there be any compatibility or configuration issues I should be aware of?
I’m still a beginner with DLLs and C/C++ integration in PLECS, so any guidance or setup tips would be really appreciated.
There is a VS Code based workflow which I myself use. However you need additional tooling beyond just VS Code, which is basically a source-code editor with powerful extensions. You need to choose a compiler and build workflow.
What OS are you using? What compiler? Are you familiar with any build tools? There are many resources available on different options and how to configure each option.
Since you’re looking for tips, I can share my current setup. I primarily use MacOS with clang and with Windows I’ll use MSVC or gcc. Each one of those tools has their own command-line make utilities. Since I’m often jumping between environments, for better cross platform support I’ll use CMake via the VS Code extension.
If you are able to get your tooling up, here is an example CMakeLists.txt file that works with the PLECS: Using the DLL block. This build file is cross-platform so that you can easily use the same tooling with multiple operating systems, compilers, and architectures. You can copy this text to a CMakeLists.txt file in the dll_block folder associated with the tutorial and go from there. Much of this file is for cross platform support, so you can also pare it down for use with your usual environment.
# Example CMakeLists.txt file for use with the "PLECS: Using the DLL block" tutorial
cmake_minimum_required(VERSION 3.0.0)
project(DLL_BLOCK_DEMO VERSION 0.1.0 LANGUAGES C)
# Set platform-specific configurations
if(APPLE)
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "Build architectures for macOS" FORCE)
set(CMAKE_SHARED_LIBRARY_PREFIX "") # Avoid 'lib' prefix on macOS
set(LIB_EXTENSION ".dylib")
elseif(WIN32)
set(LIB_EXTENSION ".dll")
else() # Linux and other Unix-like systems
set(CMAKE_SHARED_LIBRARY_PREFIX "") # Avoid 'lib' prefix on all platforms
set(LIB_EXTENSION ".so")
endif()
# Add the shared library
add_library(pi_controller SHARED "${PROJECT_SOURCE_DIR}/pi_controller.c")
# Include directories (add your includes here)
target_include_directories(pi_controller PUBLIC
"${PROJECT_SOURCE_DIR}/include" # Assuming your headers are in an 'include' directory
)
# Set output directory based on platform and build type
set(OUTPUT_DIR "${PROJECT_SOURCE_DIR}")
set_target_properties(pi_controller PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIR}"
ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_DIR}"
PREFIX "" # No prefix for all platforms
OUTPUT_NAME "pi_controller"
)
# Set compiler flags based on platform and compiler
if(MSVC) # Visual Studio
target_compile_options(pi_controller PRIVATE /W4)
# Define export symbols for Windows DLL
target_compile_definitions(pi_controller PRIVATE "DLL_EXPORT")
else() # GCC, Clang
target_compile_options(pi_controller PRIVATE -Wall -Wextra)
endif()
# Packaging
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)