1

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 ?

Bazziil
  • 39
  • 1
  • 5
  • 3
    You are calling `read_int()` but have not defined it. See https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix?rq=1 for a lengthy explanation. – Adriaan de Groot Jan 25 '21 at 10:51
  • 1
    .. so to answer the "Should I create a custom makefile?" question part, the answer would be "no, since the problem is neither in CMake or the Makefile that it generates." – Adriaan de Groot Jan 25 '21 at 10:52
  • Thanks, it is, as i suspected a simple mistake, in my read.h file i changed the `int read_int( ... );` to `int read_int( ... ) {}` and it worked – Bazziil Jan 25 '21 at 11:18
  • I could easily changed it in my .h but what about the .so files ? – Bazziil Jan 25 '21 at 12:23
  • Please create a new question. Your additional question has nothing to do with your original question, so shouldn't be tacked on to the end. When you ask the new question please provide details. Saying "it doesn't work" is useless to us for understanding the problem. Please provide the command you entered and the errors you got. – MadScientist Jan 25 '21 at 13:46
  • Ok, [link](https://stackoverflow.com/questions/65886495/how-to-link-so-files-with-cmake) <-- link to the topic. – Bazziil Jan 25 '21 at 14:21
  • I'd discourage adding the implementation to a header file unless you have a reason to do so (e.g. inline functions, templates ...). Otherwise you may end up with a linker error, if you include the header in multiple .cpp files. The standard approach would be to leave the header with the declaration and adding the implementation in a cpp file (`add_executable(${PROJECT_NAME} main_new.cpp read.cpp)`) or linking a library that provides the function. – fabian Jan 25 '21 at 21:38

0 Answers0