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

相关文章

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

Java SWT库详解与安装指南(最新推荐)

《JavaSWT库详解与安装指南(最新推荐)》:本文主要介绍JavaSWT库详解与安装指南,在本章中,我们介绍了如何下载、安装SWTJAR包,并详述了在Eclipse以及命令行环境中配置Java... 目录1. Java SWT类库概述2. SWT与AWT和Swing的区别2.1 历史背景与设计理念2.1.

安装centos8设置基础软件仓库时出错的解决方案

《安装centos8设置基础软件仓库时出错的解决方案》:本文主要介绍安装centos8设置基础软件仓库时出错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录安装Centos8设置基础软件仓库时出错版本 8版本 8.2.200android4版本 javas

Pytorch介绍与安装过程

《Pytorch介绍与安装过程》PyTorch因其直观的设计、卓越的灵活性以及强大的动态计算图功能,迅速在学术界和工业界获得了广泛认可,成为当前深度学习研究和开发的主流工具之一,本文给大家介绍Pyto... 目录1、Pytorch介绍1.1、核心理念1.2、核心组件与功能1.3、适用场景与优势总结1.4、优

conda安装GPU版pytorch默认却是cpu版本

《conda安装GPU版pytorch默认却是cpu版本》本文主要介绍了遇到Conda安装PyTorchGPU版本却默认安装CPU的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、问题描述二、网上解决方案罗列【此节为反面方案罗列!!!】三、发现的根本原因[独家]3.1 p