Haskell 安装 Cairo

2023-11-29 03:20
文章标签 安装 haskell cairo

本文主要是介绍Haskell 安装 Cairo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

背景

Haskell 项目需要使用到柱状图,折线图等(demo 代码)

步骤(默认已安装 stack, cabal, ghcup)

nameversion
stack2.11.1
cabal3.8.1.0
ghcup0.1.20.0
  1. 在 package.yaml 中添加所需依赖 Chart 和 Chart-cairo
name:                a
version:             0.1.0.0
github:              "githubuser/a"
license:             BSD-3-Clause
author:              "Author name here"
maintainer:          "example@example.com"
copyright:           "2023 Author name here"extra-source-files:
- README.md
- CHANGELOG.md# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/a#readme>dependencies:
- base >= 4.7 && < 5ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraintslibrary:source-dirs: - src- appdependencies: - bytestring- http-conduit- sqlite-simple- Chart- text# - Chart-diagrams# - diagrams# - diagrams-svg# - diagrams-lib- colour- data-default-class# common- Chart-cairo- lens- aeson- stm- split- text- time- binary- binary-orphans- old-locale- directory- pretty-simple- string-conversionsexecutables:a-exe:main:                Main.hssource-dirs:         appghc-options:- -threaded- -rtsopts- -with-rtsopts=-Ndependencies:- a- sqlite-simple- Chart- text- bytestring# - Chart-diagrams# - diagrams# - diagrams-svg# - diagrams-lib- colour- data-default-class# common- Chart-cairo- lens- aeson- stm- split- text- time- binary- binary-orphans- old-locale- directory- pretty-simple- string-conversionstests:a-test:main:                Spec.hssource-dirs:         testghc-options:- -threaded- -rtsopts- -with-rtsopts=-Ndependencies:- a

运行 stack-build,编译失败,大概提示未指定 cairo 版本。

  1. 在 stack.yaml 中添加 chart 版本信息
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
# resolver: lts-21.13
resolver: lts-21.13# url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/22.yaml# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
#   subdirs:
#   - auto-update
#   - wai
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
#   commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
# extra-deps: []# Override default flag values for local packages and extra-deps
# flags: {}# Extra package databases containing global packages
# extra-package-dbs: []# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of Stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=2.11"
#
# Override the architecture used by Stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by Stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
extra-deps:
- Chart-1.9.5
- Chart-cairo-1.9.4.1flags: {}extra-package-dbs: []

运行 stack build,报错。

ErrorTrace:

cairo      > configure
cairo      > [1 of 3] Compiling Main             ( C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\Setup.hs, C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\Main.o )
cairo      > [2 of 3] Compiling StackSetupShim   ( C:\sr\setup-exe-src\setup-shim-9p6GVs8J.hs, C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\StackSetupShim.o )
cairo      > [3 of 3] Linking C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\setup.exe
cairo      > Configuring cairo-0.13.10.0...
cairo      > Error: setup.EXE: The pkg-config package 'cairo' version >=1.2.0 is required
cairo      > but it could not be found.
cairo      >
Progress 1/3Error: [S-7282]Stack failed to execute the build plan.While executing the build plan, Stack encountered the error:[S-7011]While building package cairo-0.13.10.0 (scroll up to its section to see the error) using:C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\setup --verbose=1 --builddir=.stack-work\dist\22605e11 configure --with-ghc=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\ghc-9.4.7\bin\ghc-9.4.7.exe --with-ghc-pkg=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\ghc-9.4.7\bin\ghc-pkg-9.4.7.exe --user --package-db=clear --package-db=global --package-db=C:\sr\snapshots\9095fbbf\pkgdb --libdir=C:\sr\snapshots\9095fbbf\lib --bindir=C:\sr\snapshots\9095fbbf\bin --datadir=C:\sr\snapshots\9095fbbf\share --libexecdir=C:\sr\snapshots\9095fbbf\libexec --sysconfdir=C:\sr\snapshots\9095fbbf\etc --docdir=C:\sr\snapshots\9095fbbf\doc\cairo-0.13.10.0 --htmldir=C:\sr\snapshots\9095fbbf\doc\cairo-0.13.10.0 --haddockdir=C:\sr\snapshots\9095fbbf\doc\cairo-0.13.10.0 --dependency=Cabal=Cabal-3.8.1.0 --dependency=array=array-0.5.4.0 --dependency=base=base-4.17.2.0 --dependency=bytestring=bytestring-0.11.5.2 --dependency=gtk2hs-buildtools=gtk2hs-buildtools-0.13.10.0-7OxGn4htize3gcv93EYR9n --dependency=mtl=mtl-2.2.2 --dependency=text=text-2.0.2 --dependency=utf8-string=utf8-string-1.0.2-KqazO8nUWrDMJ2shK7HoQ -fcairo_pdf -fcairo_ps -fcairo_svg --ghc-options -haddock --extra-include-dirs=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\include --extra-lib-dirs=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\lib --extra-lib-dirs=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\bin --exact-configuration --ghc-option=-fhide-source-pathsProcess exited with code: ExitFailure 1

提示 gcc 自带的 pkg-config 没有高于 1.2.0 的 cairo 包

  1. 安装 MSYS2, 并配置 Path, 目的是用到 MSYS2 中的 pkg-config.
    在这里插入图片描述
    安装相关工具
# gcc
pacman -S mingw-w64-ucrt-x86_64-gcc# gtk
pacman -S mingw-w64-x86_64-gtk4# 语言开发包
pacman -S mingw-w64-x86_64-toolchain base-devel
  1. 重启 PowerShell 和 VSCode
  2. 在 VSCode 的 PowerShell 中运行
$Env:PATH -split ';'PS C:\Users\df366\a\a> $Env:PATH -split ';'
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Windows\System32\OpenSSH\
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR
C:\Program Files\dotnet\
C:\Program Files\Docker\Docker\resources\bin
D:\Git\cmd
D:\MSYS2\mingw64\bin
D:\MSYS2\mingw64\lib
D:\MSYS2\mingw64\include
C:\Users\df366\AppData\Roaming\local\bin
C:\Users\df366\AppData\Local\Microsoft\WindowsApps
C:\Users\df366\.dotnet\tools
C:\Program Files\MongoDB\Server\7.0\bin
D:\Microsoft VS Code\bin
C:\ghcup\bin
D:\PyCharm 2023.2.2\bin

注意:这里必须重启 VSCode,否则系统环境中的变量不会更新。

  1. 检查 pkg-config
Get-Command pkg-configCommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     pkg-config.exe                                     0.0.0.0    D:\MSYS2\mingw64\bin\pkg-config.exe

发现 pkg-config 已经变成 MSYS2 下的了

  1. 重新执行 stack build,运行成功
PS C:\Users\df366\a\a> stack build
a-0.1.0.0: unregistering (local file changes: app\Main.hs)
a> build (lib + exe)
Preprocessing library for a-0.1.0.0..
Building library for a-0.1.0.0..
[6 of 7] Compiling Main [Source file changed]app\Main.hs:3:1: warning: [-Wunused-imports]The import of 鈥楩etch鈥?is redundantexcept perhaps to import instances from 鈥楩etch鈥?[0mTo import instances alone, use: import Fetch()|
3 | import Fetch| ^^^^^^^^^^^^app\Main.hs:4:1: warning: [-Wunused-imports]The import of 鈥楧ata.Text鈥?is redundantexcept perhaps to import instances from 鈥楧ata.Text鈥?[0mTo import instances alone, use: import Data.Text()|
4 | import Data.Text (Text, splitOn)| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^app\Main.hs:5:1: warning: [-Wunused-imports]The qualified import of 鈥楧ata.Text鈥?is redundantexcept perhaps to import instances from 鈥楧ata.Text鈥?[0mTo import instances alone, use: import Data.Text()|
5 | import qualified Data.Text as T| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^app\Main.hs:6:1: warning: [-Wunused-imports]The import of 鈥楧atabase鈥?is redundantexcept perhaps to import instances from 鈥楧atabase鈥?[0mTo import instances alone, use: import Database()|
6 | import Database| ^^^^^^^^^^^^^^^
[7 of 7] Linking app\Main.exe [Objects changed]
Preprocessing executable 'a-exe' for a-0.1.0.0..
Building executable 'a-exe' for a-0.1.0.0..
[1 of 2] Compiling Main [Source file changed]app\Main.hs:3:1: warning: [-Wunused-imports]The import of 鈥楩etch鈥?is redundantexcept perhaps to import instances from 鈥楩etch鈥?[0mTo import instances alone, use: import Fetch()|
3 | import Fetch| ^^^^^^^^^^^^app\Main.hs:4:1: warning: [-Wunused-imports]The import of 鈥楧ata.Text鈥?is redundantexcept perhaps to import instances from 鈥楧ata.Text鈥?[0mTo import instances alone, use: import Data.Text()|
4 | import Data.Text (Text, splitOn)| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^app\Main.hs:5:1: warning: [-Wunused-imports]The qualified import of 鈥楧ata.Text鈥?is redundantexcept perhaps to import instances from 鈥楧ata.Text鈥?[0mTo import instances alone, use: import Data.Text()|
5 | import qualified Data.Text as T| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^app\Main.hs:6:1: warning: [-Wunused-imports]The import of 鈥楧atabase鈥?is redundantexcept perhaps to import instances from 鈥楧atabase鈥?[0mTo import instances alone, use: import Database()|
6 | import Database| ^^^^^^^^^^^^^^^
[3 of 3] Linking .stack-work\dist\22605e11\build\a-exe\a-exe.exe [Objects changed]
a> copy/register
Installing library in C:\Users\df366\a\a\.stack-work\install\1b692ffa\lib\x86_64-windows-ghc-9.4.7\a-0.1.0.0-5LEhwaAmmakDiuPZZmv67T
Installing executable a-exe in C:\Users\df366\a\a\.stack-work\install\1b692ffa\bin
Registering library for a-0.1.0.0..
  1. Main 代码
module Main (main) whereimport Fetch
import Data.Text (Text, splitOn)
import qualified Data.Text as T
import Database
-- import Diagrams.Prelude
-- import Diagrams.Backend.SVG
-- import Diagrams.Attributes
-- import Diagrams.Backend.SVG.CmdLineimport Graphics.Rendering.Chart
import Graphics.Rendering.Chart.Backend.Cairo
import Data.Colour
import Control.Lens
import Data.Default.Classchart :: Bool -> Renderable ()
chart borders = toRenderable layoutwherelayout =layout_title .~ "Sample Bars" ++ btitle$ layout_title_style . font_size .~ 10$ layout_x_axis . laxis_generate .~ autoIndexAxis alabels$ layout_y_axis . laxis_override .~ axisGridHide$ layout_left_axis_visibility . axis_show_ticks .~ False$ layout_plots .~ [ plotBars bars2 ]$ def :: Layout PlotIndex Doublebars2 = plot_bars_titles .~ ["Cash","Equity"]$ plot_bars_values .~ addIndexes [[20,45],[45,30],[30,20],[70,25]]$ plot_bars_style .~ BarsClustered$ plot_bars_spacing .~ BarsFixGap 30 5$ plot_bars_item_styles .~ map mkstyle (cycle defaultColorSeq)$ defalabels = [ "Jun", "Jul", "Aug", "Sep", "Oct" ]btitle = if borders then "" else " (no borders)"bstyle = if borders then Just (solidLine 1.0 $ opaque black) else Nothingmkstyle c = (solidFillStyle c, bstyle)main :: IO ()
main = do_ <- renderableToFile def "example11_big.png" (chart True)return ()-- spilitByLine :: String -> [String]
-- spilitByLine s = map T.unpack $ splitOn (T.pack "\n") (T.pack s)-- main :: IO ()
-- main = do
--     a <- downloadURL "http://prod.publicdata.landregistry.gov.uk.s3-website-eu-west-1.amazonaws.com/pp-2022.csv"--     let items = spilitByLine a
--     print $ length items
--     print $ head items
--     print $ items !! 1
--     print 33
--     print "3.6.2.0"
  1. 输出图
    在这里插入图片描述

  2. cabal 文件(无需修改,在 package.yaml 添加依赖后,运行 stack update 会同步更新)

cabal-version: 2.2-- This file has been generated from package.yaml by hpack version 0.35.2.
--
-- see: https://github.com/sol/hpackname:           a
version:        0.1.0.0
description:    Please see the README on GitHub at <https://github.com/githubuser/a#readme>
homepage:       https://github.com/githubuser/a#readme
bug-reports:    https://github.com/githubuser/a/issues
author:         Author name here
maintainer:     example@example.com
copyright:      2023 Author name here
license:        BSD-3-Clause
license-file:   LICENSE
build-type:     Simple
extra-source-files:README.mdCHANGELOG.mdsource-repository headtype: gitlocation: https://github.com/githubuser/alibraryexposed-modules:DatabaseFetchLibTypesMainother-modules:Paths_aautogen-modules:Paths_ahs-source-dirs:srcappghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraintsbuild-depends:Chart, Chart-cairo, aeson, base >=4.7 && <5, binary, binary-orphans, bytestring, colour, data-default-class, directory, http-conduit, lens, old-locale, pretty-simple, split, sqlite-simple, stm, string-conversions, text, timedefault-language: Haskell2010executable a-exemain-is: Main.hsother-modules:Paths_aautogen-modules:Paths_ahs-source-dirs:appghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-Nbuild-depends:Chart, Chart-cairo, a, aeson, base >=4.7 && <5, binary, binary-orphans, bytestring, colour, data-default-class, directory, lens, old-locale, pretty-simple, split, sqlite-simple, stm, string-conversions, text, timedefault-language: Haskell2010test-suite a-testtype: exitcode-stdio-1.0main-is: Spec.hsother-modules:Paths_aautogen-modules:Paths_ahs-source-dirs:testghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-Nbuild-depends:a, base >=4.7 && <5default-language: Haskell2010

参考博客:
https://stackoverflow.com/questions/57940965/pkg-config-installed-but-not-found
https://stackoverflow.com/questions/44314060/haskell-stack-nix-build-fails-requiring-pkg-config-0-9-0-and-cairo-1
https://stackoverflow.com/questions/5336865/cant-install-cairo-with-cabal-on-windows-how-to-get-pkg-config-on-win
https://stackoverflow.com/questions/63396432/having-dependency-issues-while-trying-to-install-threadscope-with-stack-the-iss
https://github.com/commercialhaskell/stack/issues/377
https://stackoverflow.com/questions/77441659/haskell-after-install-the-conduit-package
https://zhuanlan.zhihu.com/p/623146940

这篇关于Haskell 安装 Cairo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/431255

相关文章

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创

linux安装、更新、卸载anaconda实践

《linux安装、更新、卸载anaconda实践》Anaconda是基于conda的科学计算环境,集成1400+包及依赖,安装需下载脚本、接受协议、设置路径、配置环境变量,更新与卸载通过conda命令... 目录随意找一个目录下载安装脚本检查许可证协议,ENTER就可以安装完毕之后激活anaconda安装更

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知

Win10安装Maven与环境变量配置过程

《Win10安装Maven与环境变量配置过程》本文介绍Maven的安装与配置方法,涵盖下载、环境变量设置、本地仓库及镜像配置,指导如何在IDEA中正确配置Maven,适用于Java及其他语言项目的构建... 目录Maven 是什么?一、下载二、安装三、配置环境四、验证测试五、配置本地仓库六、配置国内镜像地址

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

2025版mysql8.0.41 winx64 手动安装详细教程

《2025版mysql8.0.41winx64手动安装详细教程》本文指导Windows系统下MySQL安装配置,包含解压、设置环境变量、my.ini配置、初始化密码获取、服务安装与手动启动等步骤,... 目录一、下载安装包二、配置环境变量三、安装配置四、启动 mysql 服务,修改密码一、下载安装包安装地