- 主题:遇到一个错误两天没搞定
cmake_minimum_required (VERSION 3.13)
project(TLS)
#set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
#set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -march=native")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -march=native")
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
message( "========================\n")
include_directories(
${PROJECT_SOURCE_DIR}/include
)
message(${PROJECT_SOURCE_DIR}/include
)
file(GLOB SOURCES "include/*.h" "src/*.cpp")
add_executable(${PROJECT_NAME} ${SOURCES})
#target_link_libraries (${PROJECT_NAME} )
---------------------------------
//header.h
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <vector>
template<typename T>
T mone(T & data);
#endif
-----------------------------------
//header.cpp
#include "header.h"
template<typename T>
T mone(T & data)
{
return -data;
}
----------------------
#include "header.h"
int main(void)
{
int t1 = 25;
std::cout << mone(t1) << std::endl;
std::cout << t1 << std::endl;
std::vector<int> a(1); // only a reminder telling running is done.
std::cout << a[2] << std::endl;
return 0;
}
---------------
错误:
2>main.obj : error LNK2019: unresolved external symbol "int __cdecl mone<int>(int &)" (??$mone@H@@YAHAAH@Z) referenced in function _main
build\Debug\TLS.exe : fatal error LNK1120: 1 unresolved externals
--
修改:scanworld FROM 106.39.0.*
FROM 106.39.0.*
模板函数要放头文件里面
【 在 scanworld 的大作中提到: 】
: cmake_minimum_required (VERSION 3.13)
: project(TLS)
:
: ...................
--
FROM 124.64.19.*
谢谢
【 在 milksea 的大作中提到: 】
: 模板函数要放头文件里面
--
FROM 106.39.0.*