Anoop
Anoop

Reputation: 5720

Cmake cannot generate shared library, but static library is generated

I want to generate a shared library with CMake, and even though I do not get any errors, the library (.so file) is not generated.

Here is my CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(myTest)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -g3 -Wall -c -fmessage-length=0 -v -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -fPIC")


add_definitions(-DLINUX=1)

include_directories(
  ${PROJECT_SOURCE_DIR}/public/include
  # .. bunch of other include directories
  )

link_directories("${PROJECT_SOURCE_DIR}/shared/lib")


file(GLOB_RECURSE SRC_FILES
  "${PROJECT_SOURCE_DIR}/source/some1.cpp"
  "${PROJECT_SOURCE_DIR}/source/some2.cpp"
   )

add_library(myTest SHARED ${SRC_FILES})

When I create build dir, generate make files from cmake and execute the default the make target, I do not see any errors.

[ 50%] Building CXX object /path/to/some1.cpp.o
[100%] Building CXX object /path/to/some2.cpp.o 
Linking CXX shared library myTest.so
g++: warning: /path/to/some1.cpp.o: linker input file unused because linking not done
g++: warning: /path/to/some2.cpp.o: linker input file unused because linking not done
[100%] Built target myTest

But when I see in my build folder, the so file, libmytest.so is not generated. It is not generated anywhere else as I did a find on my entire system.

Surprisingly, when I change the library type to be STATIC, the libmytest.a file is generated. Am I missing something obvious here ?

Upvotes: 0

Views: 1810

Answers (1)

Anoop
Anoop

Reputation: 5720

The mistake was in the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS. For compiling from .cpp to .o, g++ needs the -c flags, but cmake will automatically add it for us.

But while generating the .so library, g++ cannot understand the meaning of -c flag. Hence it was not generating the .so file, without giving any error as well.

After removing -c flag, it was able to generate the .so shared library.

Note that for generating static library, ar archiver is used instead of g++ , hence static library was generated without being affected.

Upvotes: 1

Related Questions