clion中运行多个c++文件

clion中运行多个c++文件

方法一:使用hpp文件

1,main.cpp(主函数入口文件)

#include 
#include "i.hpp"
int main() {
    std::cout << "Hello, World!" << std::endl;
    p();
    return 0;
}

2,i.hpp

(这里用hpp是因为实现和头文件一起了所以选择用hpp格式,一般是.h文件和.cpp文件分开写)

#include 

using namespace std;

void p() {
    cout << "i.hpp!" << endl;
}

3,CMakeLists.txt

# cmake_minimum_required(VERSION )
project(test)

set(CMAKE_CXX_STANDARD 11)

add_executable(test1 main.cpp i.hpp)

编译运行输出结果:

D:\CLion\workspace\test\cmake-build-debug\test1.exe
Hello, World!
i.hpp!

这里如果i.hpp改成i.cpp会报下面的错误

====================[ Build | test1 | Debug ]===================================
"D:\CLion\CLion 2018.3.4\bin\cmake\win\bin\cmake.exe" --build D:\CLion\workspace\test\cmake-build-debug --target test1 -- -j 2
-- Configuring done
-- Generating done
-- Build files have been written to: D:/CLion/workspace/test/cmake-build-debug
Scanning dependencies of target test1
[ 33%] Building CXX object CMakeFiles/test1.dir/i.cpp.obj
[ 66%] Linking CXX executable test1.exe
d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\test1.dir/objects.a(i.cpp.obj): in function `Z1pv':
D:/CLion/workspace/test/i.cpp:7: multiple definition of `p()'; CMakeFiles\test1.dir/objects.a(main.cpp.obj):D:/CLion/workspace/test/t.hpp:75: first defined here
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [test1.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/test1.dir/all] Error 2
CMakeFiles\test1.dir\build.make:99: recipe for target 'test1.exe' failed
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/test1.dir/all' failed
CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/test1.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/test1.dir/rule] Error 2
mingw32-make.exe: *** [test1] Error 2
Makefile:117: recipe for target 'test1' failed

本以为是有多个p函数定义,但这里实际上是因为文件内容后缀不对,将i.cpp改成i.hpp即可

如果是报main函数多定义可能就是两个文件都有main函数,如果希望一个项目project生成多个可执行的程序,这时可以通过在CMakeLists.txt中多添加add_executable来实现,如下所示,win下编译最后会生成两个可执行程序,test1.exe和test2.exe

project(test)

set(CMAKE_CXX_STANDARD 11)

add_executable(test1 main.cpp i.h)
add_executable(test2 equal_range_map.cpp)

 

方法二:将i.hpp分割成i.h和i.cpp两个文件

1,i.h

#ifndef TEST_I_H
#define TEST_I_H

void p();

#endif //TEST_I_H

 

2,i.cpp

#include 

using namespace std;

void p() {
    cout << "i.cpp!" << endl;
}

3,main.cpp(主函数入口文件)

#include 
#include "i.h"  // 改变地方
int main() {
    std::cout << "Hello, World!" << std::endl;
    p();
    return 0;
}

4,CMakeLists.txt

# cmake_minimum_required(VERSION )
project(test)

set(CMAKE_CXX_STANDARD 11)

add_executable(test1 main.cpp i.h i.cpp) # 重点改变

编译运行后输出结果

D:\CLion\workspace\test\cmake-build-debug\test1.exe
Hello, World!
i.cpp!

方法三:

为了少写一些具体的文件名字,可以在CMakeLists.txt里面使用下面的方法,但这个不能有多个main函数在同一个项目project中

CMakeLists.txt

project(test)

set(CMAKE_CXX_STANDARD 11)

file(GLOB SOURCES
        *.h //*号对应具体的文件名
        *.cpp
        )
add_executable(test1 main.cpp ${SOURCE_FILES} ${SOURCES})

 

 

你可能感兴趣的:(c++)