CodeSys通过C函数接口调用Qt

1.背景介绍

建议先查看之前的文章【CodeSys中调用C语言写的动态库】,了解如何创建一个能够被codesys调用的动态库。
假如想要在函数中使用Qt或者第三方库(比如opencv等),可以在其自动生成的makefile文件中设置好相应的参数。
在这里插入图片描述

2.修改makefile

修改后的makefile:

##############################################################################
#	Copyright:		Copyright CODESYS Development GmbH
#	Program:		Extension API for the Linux SL products
##############################################################################

#
# with this makefile you can influence the build of your component 
#
#
# Hints:
#  - the name of the *Itf.m4 file in this folder will be used as name of the shared object (and component name)
#  - all C files in this folder will be compiled by default
# 

# set versions of your shared object
# must be single digit, decimal
MAJORVER=0
MINORVER=1

# set a component version (must match your IEC library version)
# must be 4 bytes, hexadecimal
CMPVERSION=0x01000000

# set a component ID 
# must be 2 bytes >= 0x2000 and hexadecimal and can be used to differentiate different components
CMPID=0x2000

# set your tools
DOS2UNIX = dos2unix
M4 = m4

# set a compiler
#CC = gcc
CC = g++


#INCLUDEPATH += /usr/include/opencv4
#LIBS += /usr/lib/x86_64-linux-gnu/libopencv_*.so          #添加库

# add some compiler flags (with += )
#CFLAGS += -g

# add some include paths (with +=)
#INCLUDES += -I.

# add some linker flags (with += )
#LDFLAGS += 

# add some libraries that you might need (with += )
#LDLIBS += -lc

# 为了能够使用opencv而做的一些设置
INCLUDES += -I/usr/include/opencv4\
			-I/usr/include/opencv4/opencv2
LDLIBS += /usr/lib/x86_64-linux-gnu/libopencv_*.so

# #为了能够使用Qt而做的一些参数设置
INCLUDES += -I/opt/Qt/5.15.2/gcc_64/include\
		    -I/opt/Qt/5.15.2/gcc_64/include/QtCore\
		    -I/opt/Qt/5.15.2/gcc_64/mkspecs/linux-g++\
			-I/opt/Qt/5.15.2/gcc_64/include/QtRemoteObjects
LDLIBS += /opt/Qt/5.15.2/gcc_64/lib/*.so
LDFLAGS += -Wl,-rpath,/opt/Qt/5.15.2/gcc_64/lib

# include of the SDK makefiles
SDKDIR=/home/yong/Desktop/PLC/ExtensionSDK
include ${SDKDIR}/makefile


主要是做了三个修改:

2.1.将编译器由c改成c++

在这里插入图片描述

2.2.使能opencv库

在这里插入图片描述

2.3.使能Qt库

在这里插入图片描述
这里特别注意一下这个:-Wl,-rpath,/opt/Qt/5.15.2/gcc_64/lib,这条语句是指定了运行时搜索库的路径,没有这句的话,虽然可以编译通过,但是运行时codesys就直接报错,无法下载了。可以看看这个资料的介绍【GCC/G++选项 -Wl,-rpath=】
我是如何知道需要写这个的?我是从QtCreator的编译输出看到的:
在这里插入图片描述

3.在代码中使用Qt库函数

这样操作之后,在include对应的头文件后,就可以在函数中使用opencv、Qt了。
在这里插入图片描述

相关推荐

  1. C调用C++接口

    2024-03-16 13:28:03       27 阅读
  2. qt c++ 子界面调用主窗口函数

    2024-03-16 13:28:03       28 阅读
  3. node通过axios调用realworld接口

    2024-03-16 13:28:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-16 13:28:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-16 13:28:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-16 13:28:03       82 阅读
  4. Python语言-面向对象

    2024-03-16 13:28:03       91 阅读

热门阅读

  1. 关系型数据库和非关系型数据库

    2024-03-16 13:28:03       39 阅读
  2. 说说JVM的class文件(二)

    2024-03-16 13:28:03       39 阅读
  3. Lua-掌握Lua语言基础1

    2024-03-16 13:28:03       30 阅读
  4. redis的安装与string类型

    2024-03-16 13:28:03       32 阅读
  5. 关于static

    2024-03-16 13:28:03       45 阅读
  6. Tomcat数据源笔记

    2024-03-16 13:28:03       41 阅读
  7. Edge浏览器可以多开的几种方法

    2024-03-16 13:28:03       37 阅读
  8. Nginx 报错 504 Gateway Time-out 的解决方法

    2024-03-16 13:28:03       36 阅读