0

I'm trying to integrate a side program (Program B) into an existing program (Program A) compiled/built with CMake. Currently CMake handles finding all the resources for and compiling Program A. I would like to include a couple .h files that Program B points to, so I can embed some of Program B's .c code in Program A.

I've tried playing around with one of the CMakeLists.txt files and even added some .c files to the add_library() block, but I still receive an "undefined reference to THIS_FUNCTION" error on compile.

Thank you for your time!

EDIT: Here's part of the CMakeList.txt file I updated on the RPi

add_library(rtlsdr_shared SHARED
    librtlsdr.c
    tuner_e4k.c
    tuner_fc0012.c
    tuner_fc0013.c
    tuner_fc2580.c
    tuner_r82xx.c
    gpu_fft.c        #Added this and a couple other .c files
)

target_link_libraries(rtlsdr_shared
    ${LIBUSB_LIBRARIES}
)

EDIT 2:

Proj A => rtl-sdr

Proj B => gpu_fft

rtl-sdr/
    CMakeLists.txt
    build/
    cmake/
    include/
    m4/
    src/
        CMakeLists.txt
        gpu_fft/
            makefile

1 Answer 1

1

I think you better should build program B as a shared library, and add to the include_directories of program A the includes of program B. Then target_link_libraries to your program A.

Edit:

What I have in mind is a project with this folder structure:

projA/
    CMakeLists.txt
    include/
    src/
    B/ 
       CMakeLists.txt
       include/
       src/

The CMakeLists.txt in B is very classical and build the shared library libB.so (e.g. on linux, .dll on WIN32).

In the CMakeLists.txt of projA folder put:

# first build project B
add_subdirectory(B)

# add include directory of project B
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/B/include")

# [...] build here your project

# and finally:
target_link_libraries(A B)

I hope it helps

EDIT2: To build shared libraries you can set:

OPTION(BUILD_SHARED_LIBS TRUE)

or simply

SET(BUILD_SHARED_LIBS TRUE)

EDIT 3: You can also use it simply as an external library. Set libraries search path first:

LINK_DIRECTORIES(${yourPathToLibB})

Don't forget to

include_directories("${yourPathToLibB}/include") 

too. And then just do

TARGET_LINK_LIBRARIES(A B)
Sign up to request clarification or add additional context in comments.

7 Comments

Interesting, I'll research it and give it a shot. One question though: When I look at the target_link_libaries above, it just has ${XXX}. What kind of format would I use to include Program B as a shared library?
it depends: are you building both ProgA and ProgB in the same project, or are they two distinct projects from the project you are building here? Maybe can they be included as add_subdirectory()?
I believe the same project. ProgA tries to include parts of ProgB's methods/functions.
See my edit: I think the best is to include B as sub-project of A.
Thank you for your help! I appreciate it! Since you seem to be the CMake guru, can I explain my situation a bit better? See my edit above, it shows the current filesystem setup. The other problem I'm running into, it that Prog B is built off a makefile (or gcc) as of now. CMake doesn't like not having a CMakeList in Program B's directory but gives this, CMake does not support this case but it used to work accidentally and is being allowed for compatibility.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.