如何指定编译器和编译不同的主要功能 [英] How to Specify the Compiler and To Compile Different Main Functions
问题描述
我的项目很简单,但我喜欢将文件保存在不同的文件夹,以清楚。例如我有三个文件夹。
My project is quite simple but I like to keep files in different folders for clarity. For instance I have three folders.
- 一个
输出
文件夹,包含用于输出的所有类。 (现在只有Output.cc和Output.h)。 - 一个
Math
文件夹,包含与数学相关的所有类。 (vector.cc,vector.h,randomgen.h,randomgen.cc等) - A
测试
cpp文件,每个包含一个main函数。 (Output_test.cc,vector_test.cc等)
- An
Output
folder that contains all the classes used for the output. (for now only Output.cc and Output.h). - A
Math
folder, containing all of the classes related to math. (vector.cc, vector.h, randomgen.h, randomgen.cc, etc) - A
Tests
folder where there are different cpp files each containing a main function. (Output_test.cc, vector_test.cc, etc)
如何创建一个符合所有主要功能的CMake脚本不同的测试程序使用不同文件夹中的类?
此外,我没有看到编译器及其选项在CMake文件中指定的位置。
How can I create a CMake script that complies all of the different main function of the different test programs using the classes that are in different folders? In addition, I didn't see where the compiler, and its options, are specified in a CMake file.
推荐答案
h1>如何指定编译器?
有几种方法可以指定要使用的编译器。设置环境变量,定义编译器变量或指定生成器。
How to specify the compiler?
There are a few ways to specify the compiler you want to use. Settings environment variables, defining compiler variables, or designating a generator.
有两种方法可以使用环境变量来帮助CMake确定使用哪个编译器一个CMake配置。使用 PATH
变量或 CC
和 CXX
变量。
There are two ways to use environment variables to help CMake determine which compiler to use during a CMake configuration. Using the PATH
variable or the CC
and CXX
variables.
确保所需编译器的路径在列表中。如果您不想修改路径,请使用第二个选项。
Make sure the path to your desired compiler is first in the list. If you don't want to modify your path, then use the 2nd option.
CMake读取变量 CC
和 CXX
,分别确定C编译器和C ++编译器的路径。注意,第一次为一个项目配置CMake时,它将缓存这些路径,并首先查找缓存以用于将来的所有配置。因此,如果您希望更改编译器路径,请务必删除缓存文件 CMakeCache.txt
。正如HughB 指出,有一个很好的例子,由 Guillaume
CMake reads the variables CC
and CXX
to determine the path for the C compiler and C++ compiler respectively. Note that the first time CMake is configured for a project it will cache these paths, and look to the cache first for all future configurations. So if you wish to change the compiler path, be sure to delete the cache file CMakeCache.txt
. As HughB pointed out, there is a good example of this given by Guillaume
和CXX,有可以在命令行定义的CMake变量来选择所需的编译器。它们是 CMAKE_C_COMPILER
和 CMAKE_CXX_COMPILER
。您可以使用 -D
选项设置它们,它们使用与CC和CXX相同的值。注意,就像CC和CXX这些在第一个CMake配置后缓存。
Similar to using CC and CXX, there are CMake variables that can be defined at the commandline to choose the desired compiler. They are CMAKE_C_COMPILER
and CMAKE_CXX_COMPILER
. You can set them using the -D
option and they use the same values as CC and CXX. Note, just like CC and CXX these are cached after the first CMake configuration.
示例
cmake -DCMAKE_CXX_COMPILER=/usr/bin/g++4.6/g++ -DCMAKE_C_COMPILER=/usr/bin/gcc4.6/gcc <path to src>
指定生成器
选择编译器是通过使用-G选项来选择一个生成器。有很多发电机可供选择,我最近回答了有关他们的问题。我不打算在这个答案中讨论太多关于他们的细节,但你可以阅读我的其他答案更多
示例
cmake -G "Unix Makefile" <path to src>
不要硬编码编译器
我建议在CMakeLists.txt文件中阻止硬编码编译器的冲动。 CMake意味着与编译器无关,你应该设置CMake文件外部的编译器信息。使用上面提到的方法之一保持您的CMake文件可移植,并避免混淆,如果自己或其他人想用不同的编译器构建项目。
Don't Hardcode the Compiler
I recommend resisting the urge to "hardcode" the compiler in the CMakeLists.txt files. CMake is meant to be compiler independent and you should be setting the compiler information external of the CMake files. Use one of the methods mentioned above to keep your CMake files portable and to avoid confusion if yourself or someone else wants to build the project with a different compiler.
相关参考。 (免责声明:Written by me)
Related references. (Disclaimer: Written by me)
- What is a CMake generator
- Understanding the Purpose Behind CMake
HughB提到使用 add_executables
。您还可以为每个文件夹组创建单独的库,有许多方法来组织项目。
As HughB mentioned use add_executables
. You could also create separate libraries for each folder group, there are many ways to organize your project. I'm going to keep it simple and give an example that builds two executables in the same project.
对于这个例子,我创建了5个文件:
For this example I created 5 files:
include / Helloworld.h
include/Helloworld.h
#include <stdio.h>
src / HelloWorld.cpp
src/HelloWorld.cpp
#include "HelloWorld.h"
int main()
{
printf("Hello World!\n");
return 0;
}
include / HelloWorld2.h
include/HelloWorld2.h
#include <stdio.h>
src / HelloWorld2.cpp
src/HelloWorld2.cpp
#include "HelloWorld2.h"
int main()
{
printf("Hello World2!\n");
return 0;
}
CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
# This is required to compile and must contain the paths to
# all of the header files
include_directories(include)
# These are optional, but are added to be passed to the
# add_executable command so that the headers are accessible
# from within the project
set(HW_INCLUDES include/HelloWorld.h)
set(HW_INCLUDES2 include/HelloWorld2.h)
project(HelloWorlds)
# Required sources
set(HW_SOURCES src/HelloWorld.cpp)
set(HW_SOURCES2 src/HelloWorld2.cpp)
add_executable(HelloWorld ${HW_SOURCES} ${HW_INCLUDES})
add_executable(HelloWorld2 ${HW_SOURCES2} ${HW_INCLUDES2})
这个项目构建时会有两个可执行文件:HelloWorld。 exe和HelloWorld2.exe。
When this project is built there will be two executables: HelloWorld.exe and HelloWorld2.exe.
这篇关于如何指定编译器和编译不同的主要功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!