Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

c++ - "Undefined reference to " after linking with CMake

I want to build a simple application based on a .cpp and a .h file. I couldn't make my project work so i started from a basic example but didn't succeed as i'm just starting creating project in Linux. From what i've seen, my CMakeLists should be like this :

My CMakeLists.txt :

cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)  
project(test C CXX)

add_executable(${PROJECT_NAME} main_new.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR})

My main_new.cpp :

#include <read.h>

int main(int argc, char* argv[])
{
    int a, b, c, d, e = 0;
    std::cout << "hello im example " << std::endl;
    read_int(&a, &b, &c, &d, &e);*
    return 0;
}

My read.h :

#ifdef __cplusplus
extern "C" {
#endif 

int read_int(int *vectorA, int *vectorB, int *matrixA, int *matrixB, int *flags);

#ifdef __cplusplus
}
#endif 

My .cpp and .h file are in the same folder. cmake . is giving me no error, but after using make i get

main_new.cpp:(.text+0x68)?: undefined reference to ??read_int??

I'm using the makefile created by the cmake command. Should i create a custom makefile ?

Edit : Added question : I also have to implement .so files, but doing target_link_libraries(test ${CMAKE_SOURCE_DIR}/libA.so ${CMAKE_SOURCE_DIR}/libB.so) in my CMakeLists.txt file doesn't work. How can i link these libraries to my executable ?

question from:https://stackoverflow.com/questions/65883076/undefined-reference-to-after-linking-with-cmake

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...