我有以下情况:我想编译一些Scheme文件Gambit https://github.com/gambit/gambit成可执行文件。为此,我使用 gambit 将所有计划文件翻译/生成为 C 和目标文件,然后将其编译并链接为可执行文件。
假设我有以下三个文件(示例取自 Gambit 文档):
/* File: "m1.c" */
int power_of_2 (int x) { return 1<<x; }
; File: "m2.scm"
(c-declare "extern int power_of_2 ();")
(define pow2 (c-lambda (int) int "power_of_2"))
(define (twice x) (cons x x))
; File: "m3.scm"
(write (map twice (map pow2 '(1 2 3 4)))) (newline)
如果我要手动编译它,则需要执行以下步骤:
$ gsc -c m2.scm # create m2.c (note: .scm is optional)
$ gsc -c m3.scm # create m3.c (note: .scm is optional)
$ gsc -link m2.c m3.c # create the incremental link file m3_.c
$ gsc -obj m1.c m2.c m3.c m3_.c # create all object files *.o
m1.c:
m2.c:
m3.c:
m3_.c:
$ gcc m1.o m2.o m3.o m3_.o -lgambit -lm -ldl -lutil # link object files
$ ./a.out
((2 . 2) (4 . 4) (8 . 8) (16 . 16))
EDIT:注意最后一个命令,这不是拼写错误,最后一步是使用gcc
用于链接。
现在,我希望使用 CMake 来为我做到这一点。这是我的概述CMakeLists.txt
:
cmake_minimum_required( VERSION 3.8...3.18 )
if( ${CMAKE_VERSION} VERSION_LESS 3.12 )
cmake_policy( VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} )
endif()
# Give this project a name
project( schemetemplate VERSION 0.0.1 )
# compile all .scm files into *.c files
# the C files as target
add_custom_target(
compiledSchemeFiles ALL
SOURCES m2.scm
m3.scm
COMMAND # this is where I call gsc(?)
VERBATIM )
# or do I use custom command?
add_custom_command( ... )
# the final executable, how to tell add_executable I want all generated files as dependency?
add_exectuable( finalexec m1.c ...)
除了纯 C/C++ 项目之外,我从未使用过 CMake。解决这个问题最明智的方法是什么?最终,我想要一个CMakeLists.txt
我可以将其放置在包含所有计划文件的子目录中,让它生成所有 C 和对象文件,并让 CMake 在父目录中使用它们来创建可执行文件和库add_executable
and add_library
.