Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构

Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构

 

本文提供了一个在Windows环境下使用Visual Studio 2013编辑现有源代码并且保持目录结构的方法。
本文使用VS2013中文社区版做示例(本版本为免费版,可在VS官网下载),其他版本的VS操作方式类似。
打开VS2013,选择【菜单】-【新建】-【从现有代码创建项目】
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第1张图片

 

选择项目类型:Visual C++
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第2张图片

项目文件位置:创建完成后会在这个目录生成sln,suo,vcxproj,filters和user等VS解决方案和项目文件,这个必须和源代码的根目录保持一致,否则不能导入文件夹结构。
项目名称填所需项目
最后去除勾选【在解决方案资源管理器中显示所有文件】,避免文件太多
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第3张图片

因为代码是Linux代码,并不需要在Windows下编译,所以选择使用外部生成系统即可
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第4张图片

无需编译的话,直接选择下一步
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第5张图片

完成项目生成向导
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第6张图片

项目生成中,如果文件较多,会需要一些时间
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第7张图片

如果源文件来源于网络映射盘(比如笔者这里使用的是Linux的Samba共享磁盘),会弹出安全警告,去除勾选选择确定即可。
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第8张图片

项目创建好后,在解决方案资源管理器中会出现所有筛选过后项目目录的源文件,是未分文件夹的。如果看不到解决方案资源管理器,可以在【菜单】-【视图】中打开。
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第9张图片

上面不分文件夹的源文件并不是我们想要的,如果需要分文件夹,在解决方案资源管理器中点击【显示所有文件】图标,如下图所示。点击之后,会出现文件夹。并且会在文件图标上标示出该文件是否属于项目文件。
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第10张图片

如果源代码路径文件有更新,比如增加或者删除文件,在解决方案资源管理器中点击刷新按钮,即可看到最新的文件状态。如果要将新文件添加进项目进行管理,则在新文件上点击右键,选择【包括在项目中】即可,添加后文件图标也会做出相应改变。
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第11张图片

由于源代码是Linux下,Linux的头文件与VS默认的头文件不一致,会导致很多头文件找不到或者是不匹配,影响代码提示和阅读。解决方法是添加Linux的头文件到Include路径。下面是具体操作方法:
在解决方案资源管理器中右键项目,选择【属性】。
Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第12张图片

 

在弹出的属性页中,找到VC++目录,其中的包含目录填上Linux下的头文件目录。

 

创建完成后项目文件目录会生成以下项目文件,以后需要打开项目直接双击sln解决方案文件即可

Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构_第13张图片

 

参考资料:
https://msdn.microsoft.com/zh-cn/library/b9cy3d6x.aspx

 

添加文件时的过滤选项:

*.cpp; *.hpp; *.cxx; *.hxx; *.cc; *.c; *.inl; *.h; *.hh; *.hm; *.asm; *.asmx; *.rc; *.resx; *.idl; *.rc2; *Makefile; Makefile*; *makefile; makefile*; CMakeLists.*; *.cmake; *Config; *config; *.am; *.ac; *.in; *.mk; *config; *.inc; *.lds; *.s; *.S; *.def; *.odl; *.xsd; *.bin; *.rgs; *.html; *.htm; *.d; *.txt; *.text; *.lic; *.ini; *.conf; *.xml; *.json; *.ini; *.lang; *.properties; .gitignore; *.bat; *.ps; *.sh; *.bash; *.csh; *.pl; *.pm; *.plx; *.md; *.markdown; .gitconfig; .gitignore; .gitmodules; .gitlab-ci.yml; _clang-format; LICENSE; COPYRIGHT; VERSION; RELEASE; CHANGELOG

 

Include 包含的路径:

W:\root\niaoyun\qqwry\src\evpp
W:\root\niaoyun\qqwry\src
W:\root\niaoyun\qqwry\src\include
W:\root\niaoyun\qqwry\include
W:\usr\include\c++\4.8.2
W:\usr\include\c++\4.8.2\x86_64-redhat-linux
W:\usr\include\c++\4.8.2\backward
W:\usr\lib\gcc\x86_64-redhat-linux\4.8.2\include
W:\usr\local\include
W:\usr\include

 

预定义的宏开关:

__LP64__
__x86_64__
_UNICODE
UNICODE
__linux
__linux__
__gnu_linux__
__unix
__unix__
__GNUC__
DEBUG
_DEBUG_CDB

 

预取消的宏开关:

_MSC_VER
_WIN32
_WIN64
_WINDOWS
_WIN32_WINDOWS
_WIN32_WINNT
NDEBUG

 

CMakeLists.txt 的简单模板:

 

#
# Filename: CMakeLists.txt
# Usage:
#     $ mkdir build
#     $ cd build
#     $ cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_COLOR_MAKEFILE=ON ../src
#     $ gmake V=1 all
#

cmake_minimum_required (VERSION 2.8)

set(PROJECT_NAME "qqwry")
project(${PROJECT_NAME} C CXX)

set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_VERSION_PATCH 1)
set(PROJECT_VERSION_TWEAK 1)

enable_testing()

message(STATUS "operation system is ${CMAKE_SYSTEM}")

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
    message(STATUS "current platform: Linux")
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
    message(STATUS "current platform: Windows")
elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
    message(STATUS "current platform: FreeBSD")
else ()
    message(STATUS "other platform: ${CMAKE_SYSTEM_NAME}")
endif (CMAKE_SYSTEM_NAME MATCHES "Linux")

if (WIN32)
    message(STATUS "Now is windows")
    message(STATUS "Note: Define UNICODE environment variable")
endif (WIN32)

if (APPLE)
    message(STATUS "Now is Apple systens.")
endif (APPLE)

if (UNIX)
    message(STATUS "Now is UNIX-like OS's. Including APPLE OS x and CygWin")
endif (UNIX)


if (CMAKE_BUILD_TYPE MATCHES "Debug" OR CMAKE_BUILD_TYPE MATCHES "None")
    message(STATUS "CMAKE_BUILD_TYPE is Debug")
elseif (CMAKE_BUILD_TYPE MATCHES "Release")
    message(STATUS "CMAKE_BUILD_TYPE is Release")
elseif (CMAKE_BUILD_TYPE MATCHES "RelWitchDebInfo")
    message(STATUS "CMAKE_BUILD_TYPE is RelWitchDebInfo")
elseif (CMAKE_BUILD_TYPE MATCHES "MinSizeRel")
    message(STATUS "CMAKE_BUILD_TYPE is MinSizeRel")
else ()
    message(FATAL_ERROR "Error: unknown CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE},"
            " CMAKE_BUILD_TYPE: Debug or None, Release, RelWitchDebInfo, RelWitchDebInfo")
endif ()


if (NOT CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_COMPILER_IS_GNUCXX AND NOT MSVC)
    message(FATAL_ERROR "Error: ${CMAKE_C_COMPILER} need requires gcc/g++ or msvc.")
else (NOT CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_COMPILER_IS_GNUCXX AND NOT MSVC)
    message(STATUS "Note: c compiler - ${CMAKE_C_COMPILER}")
    message(STATUS "Note: cxx compiler - ${CMAKE_CXX_COMPILER}")
endif (NOT CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_COMPILER_IS_GNUCXX AND NOT MSVC)

if (CMAKE_COMPILER_IS_GNUCC)
    EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GNUCC_VERSION)
    if (GNUCC_VERSION VERSION_LESS 4.7)
        message(FATAL_ERROR "Error: GNUCC need requires gcc 4.7 or greater.")
    else (GNUCC_VERSION VERSION_LESS 4.7)
        message(STATUS "Note: GNUCC current version ${GNUCC_VERSION}")
    endif (GNUCC_VERSION VERSION_LESS 4.7)
endif (CMAKE_COMPILER_IS_GNUCC)

if (CMAKE_COMPILER_IS_GNUCXX)
    EXECUTE_PROCESS(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GNUCXX_VERSION)
    if (GNUCXX_VERSION VERSION_LESS 4.7)
        message(FATAL_ERROR "Error: GNUCXX need requires g++ 4.7 or greater.")
    else (GNUCXX_VERSION VERSION_LESS 4.7)
        message(STATUS "Note: GNUCXX current version ${GNUCXX_VERSION}")
    endif (GNUCXX_VERSION VERSION_LESS 4.7)
endif (CMAKE_COMPILER_IS_GNUCXX)

if (MSVC)
    if (MSVC_VERSION LESS 1700)
        message(FATAL_ERROR "Error: MSVC need requires vs2012 or greater.")
    else (MSVC_VERSION LESS 1700)
        message(STATUS "Note: MSVC current version ${MSVC_VERSION}")
    endif (MSVC_VERSION LESS 1700)
endif (MSVC)


set(PROJECT_DIST_DIR ${PROJECT_SOURCE_DIR}/../dist)
file(MAKE_DIRECTORY ${PROJECT_DIST_DIR} ${PROJECT_DIST_DIR}/bin ${PROJECT_DIST_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_DIST_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_DIST_DIR}/lib)

include_directories(/${PROJECT_NAME}/include /opt/${PROJECT_NAME}/include /usr/local/${PROJECT_NAME}/include)
link_directories(/${PROJECT_NAME}/lib /opt/${PROJECT_NAME}/lib /usr/local/${PROJECT_NAME}/lib)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR})
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../lib ${CMAKE_CURRENT_SOURCE_DIR}/lib ${CMAKE_CURRENT_SOURCE_DIR})

if (CMAKE_BUILD_TYPE MATCHES "Debug" OR CMAKE_BUILD_TYPE MATCHES "None")
    message(STATUS "Note: Define debug environment variable")
    add_definitions(-D _DEBUG_CDB)
    add_definitions(-D DEBUG)
    add_definitions(-D VALGRIND)
    set(MY_EXECUTABLE_NAME "${PROJECT_NAME}_debug.exe")
    set(MY_LIBRARY_NAME "${PROJECT_NAME}_debug.lib")
else (CMAKE_BUILD_TYPE MATCHES "Debug" OR CMAKE_BUILD_TYPE MATCHES "None")
    message(STATUS "Note: Define non debug environment variable")
    add_definitions(-D NDEBUG)
    set(MY_EXECUTABLE_NAME "${PROJECT_NAME}.exe")
    set(MY_LIBRARY_NAME "${PROJECT_NAME}.lib")
endif (CMAKE_BUILD_TYPE MATCHES "Debug" OR CMAKE_BUILD_TYPE MATCHES "None")

if (WIN32)
    message(STATUS "Note: Define UNICODE environment variable")
    add_definitions(-D _UNICODE)
    add_definitions(-D UNICODE)
endif (WIN32)

if (CMAKE_COMPILER_IS_GNUCC)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -std=c99")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wfloat-equal -funsigned-char")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-variable -Wno-unused-parameter -Wno-reorder -Wno-unused-function -Wno-unused-local-typedefs")
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s -O3 -Os -fno-omit-frame-pointer -fno-strict-aliasing -finline-limit=1000")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g -ggdb -pipe -ffast-math")
    set(CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT} ")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--export-dynamic")
endif (CMAKE_COMPILER_IS_GNUCC)

if (CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++11")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wfloat-equal -funsigned-char")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-variable -Wno-unused-parameter -Wno-reorder -Wno-unused-function -Wno-unused-local-typedefs")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wold-style-cast -Woverloaded-virtual -Wwrite-strings")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -s -O3 -Os -fno-omit-frame-pointer -fno-strict-aliasing -finline-limit=1000")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g -ggdb -pipe -ffast-math")
    set(CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT} ")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--export-dynamic")
endif (CMAKE_COMPILER_IS_GNUCXX)

if (MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /wd\"4819\" /wd\"4996\"")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Zi /Ob0 /Od /RTC1 /D_DEBUG")
    set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /O1 /Ob1 /D NDEBUG")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2 /Ob2 /D NDEBUG")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Zi /O2 /Ob1 /D NDEBUG")
    set(CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT} ")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ")
endif (MSVC)

if(CMAKE_BUILD_BITS EQUAL 32)
    message(STATUS "Note: Cmake build bits is 32.")
    message(FATAL_ERROR "Error: Stop building here - can not building 32bit programs")

    if (CMAKE_COMPILER_IS_GNUCC)
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32 -march=i386 -mtune=i386")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} –m elf_i386")
    endif (CMAKE_COMPILER_IS_GNUCC)

    if (CMAKE_COMPILER_IS_GNUCXX)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -march=i386 -mtune=i386")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} –m elf_i386")
    endif (CMAKE_COMPILER_IS_GNUCXX)

elseif (CMAKE_BUILD_BITS EQUAL 64)
    message(STATUS "Note: Cmake build bits is 64.")

else ()
    message(STATUS "Note: Cmake build bits is unknown.")

endif ()


if (CMAKE_COMPILER_IS_GNUCC)
    # add_definitions(-D __STDC_LIMIT_MACROS)
    # add_definitions(-D __STDC_CONSTANT_MACROS)
    # add_definitions(-D __STDC_FORMAT_MACROS)
endif (CMAKE_COMPILER_IS_GNUCC)

if (CMAKE_COMPILER_IS_GNUCXX)
    # add_definitions(-D MAIN_USE_ENVP)
endif (CMAKE_COMPILER_IS_GNUCXX)

if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
    # add_definitions(-D MAIN_USE_ENVP)
    add_definitions(-D CHECK_PTHREAD_RETURN_VALUE)
    add_definitions(-D _FILE_OFFSET_BITS=64)
endif (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)

if (MSVC)
    # add_definitions(-D _AFX_SECURE_NO_WARNINGS)
    # add_definitions(-D _ATL_SECURE_NO_WARNINGS)
    # add_definitions(-D _CRT_SECURE_NO_WARNINGS)
    # add_definitions(-D _CRT_NONSTDC_NO_WARNINGS)
    # add_definitions(-D _SCL_SECURE_NO_WARNINGS)
    # add_definitions(-D NOMINMAX)
endif (MSVC)


message(STATUS ">>>>>> Print cmake environment variable information ...")

message(STATUS "This is the cmake sizeof void p: ${CMAKE_SIZEOF_VOID_P}")
message(STATUS "This is the cmake module path: ${CMAKE_MODULE_PATH}")
message(STATUS "This is the cmake build bits: ${CMAKE_BUILD_BITS}")
message(STATUS "This is the cmake current list file: ${CMAKE_CURRENT_LIST_FILE}")
message(STATUS "This is the cmake current list line: ${CMAKE_CURRENT_LIST_LINE}")
message(STATUS "This is the cmake archive output directory: ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")
message(STATUS "This is the cmake library output directory: ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
message(STATUS "This is the cmake runtime output directory: ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
message(STATUS "This is the project name: ${PROJECT_NAME}")
message(STATUS "This is the project binary dir: ${PROJECT_BINARY_DIR}")
message(STATUS "This is the project source dir: ${PROJECT_SOURCE_DIR}")
message(STATUS "This is the executable output path: ${EXECUTABLE_OUTPUT_PATH}")
message(STATUS "This is the build shared libs: ${BUILD_SHARED_LIBS}")
message(STATUS "This is the library output path: ${LIBRARY_OUTPUT_PATH}")

message(STATUS "This is the cmake c compiler: ${CMAKE_C_COMPILER}")
message(STATUS "This is the cmake cxx compiler: ${CMAKE_CXX_COMPILER}")
message(STATUS "This is the cmake c compiler object: ${CMAKE_C_COMPILE_OBJECT}")
message(STATUS "This is the cmake cxx compiler object: ${CMAKE_CXX_COMPILE_OBJECT}")

string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
message(STATUS "C_FLAGS = " ${CMAKE_C_FLAGS} " " ${CMAKE_C_FLAGS_${BUILD_TYPE}})
message(STATUS "CXX_FLAGS = " ${CMAKE_CXX_FLAGS} " " ${CMAKE_CXX_FLAGS_${BUILD_TYPE}})
message(STATUS "EXE_LINKER_FLAGS = " ${CMAKE_EXE_LINKER_FLAGS} " " ${CMAKE_EXE_LINKER_FLAGS_${BUILD_TYPE}})

message(STATUS "<<<<<< End (Print cmake environment variable information ...)")


set(QQWRY_SRCS
qqwry.cpp
glog_helper.cpp
daemon_helper.cpp
)

include_directories(/${PROJECT_NAME}/include/evpp /opt/${PROJECT_NAME}/include/evpp /usr/local/${PROJECT_NAME}/include/evpp)
link_directories(/${PROJECT_NAME}/lib/evpp /opt/${PROJECT_NAME}/lib/evpp /usr/local/${PROJECT_NAME}/lib/evpp)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include/evpp ${CMAKE_CURRENT_SOURCE_DIR}/include/evpp ${CMAKE_CURRENT_SOURCE_DIR}/evpp)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../lib/evpp ${CMAKE_CURRENT_SOURCE_DIR}/lib/evpp ${CMAKE_CURRENT_SOURCE_DIR}/evpp)

add_executable(${MY_EXECUTABLE_NAME} ${QQWRY_SRCS})
target_link_libraries(${MY_EXECUTABLE_NAME} evpp event glog pthread)

if(NOT CMAKE_BUILD_NO_EXAMPLES)
    add_subdirectory(tests)
endif()

 

CentOS7 的ld.so.conf 文件的内容:

# cat /etc/ld.so.conf
/usr/lib
/usr/lib64
/usr/libexec
/usr/local/lib
/usr/lcoal/lib64
/usr/local/libexec
include ld.so.conf.d/*.conf

 

你可能感兴趣的:(Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构)