0

I'm learning about Cmake and adding library projects in simple C projects. I have created a simple Library project with the following structure.

MyLibraryProjectDirectory
    |
    -- library.C
    -- library.h
    -- UmerDir
          |
          -- Umer.h
          -- Umer.c

The Cmakelist.txt is generating a .a lib file that I'm using in another C program.

The C Program is structured like this:

MyCProgram
   |
   -- main.c
   -- libuntitled.a

libuntitled.a is the library file generated by CmakeList.txt in my library project.

The problem is when I try to build the C project with the .a file, it throws the following

ERRORS:

"C:\Program Files\JetBrains\CLion 2017.2\bin\cmake\bin\cmake.exe" --build C:\Users\Lenovo\CLionProjects\AppProject\cmake-build-debug --target AppProject -- -j 2
[ 50%] Linking C executable AppProject.exe
C:/mingw-w64/i686-7.1.0-posix-dwarf-rt_v5-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.1.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -llibuntitled
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [AppProject.exe] Error 1
CMakeFiles\AppProject.dir\build.make:95: recipe for target 'AppProject.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/AppProject.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/AppProject.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/AppProject.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/AppProject.dir/rule' failed
mingw32-make.exe: *** [AppProject] Error 2
Makefile:117: recipe for target 'AppProject' failed

I'm not sure what I'm doing wrong, so please excuse my extremely limited knowledge in CMake and C build systems.

I'm also attaching the screenshots of the C Program and C library Project for convenience.

Library Project Screenshots:

enter image description here enter image description here enter image description here enter image description here

C Program Project:

enter image description here enter image description here

EDIT:

I have fixed the library not found problem by adding ${CMAKE_SOURCE_DIR} before .a library name. Apparently Cmake needed the full classified path to locate the .a file even in the same directory, which is kinda weird. Anyways, now I'm getting the following error:

ERROR:

"C:\Program Files\JetBrains\CLion 2017.2\bin\cmake\bin\cmake.exe" --build C:\Users\Lenovo\CLionProjects\AppProject\cmake-build-debug --target AppProject -- -j 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/AppProject.dir/all' failed
mingw32-make.exe[3]: *** No rule to make target '../libuntitled', needed by 'AppProject.exe'.  Stop.
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/AppProject.dir/rule' failed
mingw32-make.exe[2]: *** [CMakeFiles/AppProject.dir/all] Error 2
Makefile:117: recipe for target 'AppProject' failed
mingw32-make.exe[1]: *** [CMakeFiles/AppProject.dir/rule] Error 2
mingw32-make.exe: *** [AppProject] Error 2

mingw32-make.exe3: No rule to make target '../libuntitled', needed by 'AppProject.exe'. Stop.

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
  • `target_link_libraries(AppProject untitled)` not "libuntitled". – utopia Jul 21 '17 at 11:46
  • The problem is resolved. Please see the answer below. I can't accept my own answer as accepted answer yet – Umer Farooq Jul 21 '17 at 11:48
  • Possible duplicate of [How do I tell CMake to link in a static library in the source directory?](https://stackoverflow.com/questions/14077611/how-do-i-tell-cmake-to-link-in-a-static-library-in-the-source-directory) – Mike Kinghan Jul 22 '17 at 17:26

3 Answers3

1

Aside from the fact, that I wouldn't create a library in the first CMake-Project and link it manually in the second. Propably this will help you: How do I tell CMake to link in a static library in the source directory?

But you should look (if possible) for a way to create a single CMake-Project with sub-projects.. Here a link to a good explanation: http://techminded.net/blog/modular-c-projects-with-cmake.html

Macxx
  • 85
  • 4
  • I understand. The reason I'm doing it this way, is because I have another huge C project that I'll be using to make a .a file and then use that .a file inside anther C program – Umer Farooq Jul 21 '17 at 11:29
  • I understood your problem.. The problem of independant projects is if any changes were made on your *huge* library, you have to compile this library manually, bevor using it in your c executable program. In a single project, CMake is checking all dependencies, if changes were made and recompiles them, if so. – Macxx Jul 21 '17 at 11:47
0

I'm unfamiliar with cmake, but it should be generating -luntitled, not -llibuntitled, to link libuntitled.a

Mischa
  • 2,240
  • 20
  • 18
0

I have fixed the library not found problem by adding ${CMAKE_SOURCE_DIR} before .a library name. Apparently Cmake needed the full classified path to locate the .a file even in the same directory, which is kinda weird.

CmakeList.txt now looks like this

target_link_libraries(AppProject ${CMAKE_SOURCE_DIR}/${MY_LIB})
Umer Farooq
  • 7,356
  • 7
  • 42
  • 67