我正在 Linux Fedora 39 上工作。我对使用 cmake 很陌生,尽管第一个好的 cmake exirsize 是从源代码构建 sqlite3。
我从官方sqlite站点下载了sqlite3.c和sqlite3.h源文件。我想将源代码编译为 .o(对象)文件,以便稍后可以将其链接到我的主应用程序。运行 cmake 不会出现错误,然后运行它生成的 make 文件不会出现错误,并表示构建已完成,但是当我查看目录时,没有 .o 文件的迹象,也没有任何已编译文件的迹象.
我知道 cmake 文件中使用的某些方法可能不是必需的(例如使用 globbing),但正如我所说,我仍在使用 cmake,看看什么对我有用并适合我未来的项目要求。
我的cmake文件:
cmake_minimum_required(VERSION 3.27)
project(SQLite3
VERSION 3.0
DESCRIPTION "SQLite3 source code"
LANGUAGES C)
# Specify your source directories
set(SOURCE_DIRS
"../source" )
# Initialize an empty list to hold all source files
set(SOURCES "")
# Set the output directory for object files
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
# Use globbing to include source files from each directory
foreach(SOURCE_DIR ${SOURCE_DIRS})
file(GLOB_RECURSE DIR_SOURCES
"${SOURCE_DIR}/*.cpp"
"${SOURCE_DIR}/*.c"
"${SOURCE_DIR}/*.cxx")
list(APPEND SOURCES ${DIR_SOURCES})
endforeach()
# Add the source files to the executable target
add_library(SQLite3 OBJECT ${SOURCES})
# ******I realise this might not be required for this case, but I tried it in any case**************
# Include the directory containing sqlite3.h if necessary
target_include_directories(SQLite3 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../headers")
# Set compiler flags based on OS
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Add compile flags before compiling
target_compile_options(SQLite3 PRIVATE
-Wall -Wextra -O2 -fPIC)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
# Add compile flags before compiling
target_compile_options(SQLite3 PRIVATE
-Wall -Wextra -O2 -fPIC)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
# Add compile flags before compiling
target_compile_options(SQLite3 PRIVATE
-Wall -Wextra -O2 -fPIC)
endif()
# Set the version of the compiler
target_compile_features(SQLite3 PRIVATE c_std_11)
我的文件结构:
注意,在生成以下输出时,我确保删除了所有生成的 cmake 文件和文件夹,以确保它是新输出,就像第一次运行一样。
Cmake 控制台输出:
-- The C compiler identification is GNU 13.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done (0.1s)
-- Generating done (0.0s)
-- Build files have been written to: /home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/build
Cmake MakeFile 控制台输出:
[100%] Building C object CMakeFiles/SQLite3.dir/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c.o
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c: In function ‘sqlite3VdbeExec’:
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96200:50: warning: this statement may fall through [-Wimplicit-fallthrough=]
96200 | zPayload[6] = (u8)(v&0xff); v >>= 8;
| ~~^~~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96201:11: note: here
96201 | case 6: zPayload[5] = (u8)(v&0xff); v >>= 8;
| ^~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96202:50: warning: this statement may fall through [-Wimplicit-fallthrough=]
96202 | zPayload[4] = (u8)(v&0xff); v >>= 8;
| ~~^~~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96203:11: note: here
96203 | case 4: zPayload[3] = (u8)(v&0xff); v >>= 8;
| ^~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96203:50: warning: this statement may fall through [-Wimplicit-fallthrough=]
96203 | case 4: zPayload[3] = (u8)(v&0xff); v >>= 8;
| ~~^~~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96204:11: note: here
96204 | case 3: zPayload[2] = (u8)(v&0xff); v >>= 8;
| ^~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96204:50: warning: this statement may fall through [-Wimplicit-fallthrough=]
96204 | case 3: zPayload[2] = (u8)(v&0xff); v >>= 8;
| ~~^~~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96205:11: note: here
96205 | case 2: zPayload[1] = (u8)(v&0xff); v >>= 8;
| ^~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96205:50: warning: this statement may fall through [-Wimplicit-fallthrough=]
96205 | case 2: zPayload[1] = (u8)(v&0xff); v >>= 8;
| ~~^~~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:96206:11: note: here
96206 | case 1: zPayload[0] = (u8)(v&0xff);
| ^~~~
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c: In function ‘jsonTranslateTextToBlob’:
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:205078:7: warning: this statement may fall through [-Wimplicit-fallthrough=]
205078 | if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){
| ^
/home/rikus/Documents/git_projects/CPP-Project/libraries/sqlite/core/source/sqlite3.c:205084:3: note: here
205084 | default: {
| ^~~~~~~
[100%] Built target SQLite3
所以基本上,根据输出,目标文件现在应该存在于我的构建目录中,但事实并非如此,我也使用文件资源管理器进行了检查。我什至尝试使用一些生成的代码来为我执行此操作,但我得到与上面完全相同的结果。
预先感谢您提供的任何帮助。
add_library
文档...
默认情况下,库文件将在与调用命令的源树目录相对应的构建树目录中创建。
您没有在 makefile 中指定构建目录,并且可能在调用 cmake 时也没有指定,因此它构建在源目录中。
如果您想在其他地方构建,请搜索“out of source build”。